1

I have a huuge HTML File, somthing like this:

<html>
 <head>

 </head>
 <body>
    <div id="wraper">
      <div id="..."> </div>
      <div id="..."> </div>
      <div id="..."> </div>
      <div class="col x8 black">
         <div class="sidebar"> 
          <script .../>
          <script .../>
          <div class="side-box last10">
           <h3 .../>
           <ul class="sidebarlist">
             <li class="fisrt"> Need this Text </li>
             <li> Need this Text too (1) </li>
             <li> Need this Text too (2) </li>
           </ul>
         </div>
      </div
    </div>
 </body>

How can I get "navigate" in this html file to get the text i want?

html->body->div#wraper->div#col x8 black-> div#side-vox last10-> ul#sidebarlist -> li#first

For this job, what is better DOM or SAX ? (I`m not a native English speaker)

1

2 Answers 2

1

Have you considered XPath? Your pseudo-code:

html->body->div#wraper->div#col x8 black-> div#side-vox last10-> 
    ul#sidebarlist -> li#first

...translates directly into the following XPath expression:

/html/body/div[@id='wraper']/div[@class='col x8 black']/
    div[@class='side-vox last10']/ul[@class='sidebarlist']/li[@class='fisrt']

Or, more succinctly (assuming the structure in your example is representative):

/html/body/div[1]/div[4]/div[1]/div[1]/ul[1]/li[1]

Information about using XPath on Android can be found here:

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @lwburk. On Android 1.6(my fault that i havent metioned if) I dont have XPath, but i read about a library that can do this stuff.
1

you can can XmlPullParser for doing that. Please go through the below code:

  public void parsing(String str1) throws XmlPullParserException, IOException{
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput( new StringReader (str1));
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            String str;
         if(eventType == XmlPullParser.START_DOCUMENT) {
            System.out.println("Start document");
         } else if(eventType == XmlPullParser.START_TAG) {
             str    =    xpp.getName();
             System.out.println("Start tag "+str);
             if(xpp.getName().equals("div")){
                 int attrCount    =    xpp.getAttributeCount();
                 if(attrCount != -1) {
                     for(int x=0;x<attrCount;x++) {
                         System.out.println("Attr Name= "+ xpp.getAttributeName(x));
                         System.out.println("Attr Value= "+ xpp.getAttributeValue(x));
                     }
                 }
            }
         } else if(eventType == XmlPullParser.END_TAG) {
             System.out.println("End tag "+xpp.getName());
         } else if(eventType == XmlPullParser.TEXT) {
             System.out.println("Value= "+xpp.getText());
         }
         eventType = xpp.next();
        }
       System.out.println("End document");
    }

5 Comments

At xpp.next() the function stop running( XMLPullParserExeption), but if i add it in a try-catch and in the catch function i repeat the function everything is perfect.
This method is to slow. There is no method like ` xpp.getChild("html").getChild("body").getValue(); ?
I don`t want to process all elements of the xml file. I want just to search for some "variables" in that xml file.
hi, if you want to get only values of some variables then you can write an if condition inside the values like: if(eventType == XmlPullParser.TEXT) { if("current_start_tag".equals("required tag")){ System.out.println("Value= "+xpp.getText());}}
Exactly this I want, but how can i get the "current_statrt_tag" ? In <div class="one" > how can I get the "class" atribute value(that will be "one")?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.