Felix
Felix

Reputation: 159

XML Parser Android

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)

Upvotes: 1

Views: 1390

Answers (2)

Wayne
Wayne

Reputation: 60414

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:

Upvotes: 1

Basil
Basil

Reputation: 2173

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");
    }

Upvotes: 1

Related Questions