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)

apaderno
  • 28,547
  • 16
  • 75
  • 90
Felix
  • 159
  • 2
  • 3
  • 13
  • 1
    Differences between SAX and DOM:http://stackoverflow.com/questions/3825206/why-is-sax-parsing-faster-than-dom-parsing-and-how-does-stax-work – Vivek Kalkur Dec 03 '11 at 09:33

2 Answers2

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");
    }
Basil
  • 2,163
  • 2
  • 16
  • 25
  • 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. – Felix Dec 03 '11 at 12:18
  • This method is to slow. There is no method like ` `xpp.getChild("html").getChild("body").getValue(); `? – Felix Dec 03 '11 at 20:31
  • I don`t want to process all elements of the xml file. I want just to search for some "variables" in that xml file. – Felix Dec 04 '11 at 19:58
  • 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());}} – Basil Dec 05 '11 at 04:14
  • Exactly this I want, but how can i get the "current_statrt_tag" ? In
    how can I get the "class" atribute value(that will be "one")?
    – Felix Dec 05 '11 at 19:06
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:

Wayne
  • 59,728
  • 15
  • 131
  • 126
  • Thanks @lwburk. On Android 1.6(my fault that i haven`t metioned if) I don`t have XPath, but i read about a library that can do this stuff. – Felix Dec 06 '11 at 20:22