XML Parser Android

断了今生、忘了曾经 提交于 2019-12-08 03:15:02

问题


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:


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:

  • http://developer.android.com/reference/javax/xml/xpath/package-summary.html



回答2:


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


来源:https://stackoverflow.com/questions/8366742/xml-parser-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!