Android XML 解析器

发布于 2024-12-19 03:18:11 字数 1010 浏览 0 评论 0原文

我有一个巨大的 HTML 文件,如下所示:

<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>

如何在这个 html 文件中“导航”以获取我想要的文本?

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

对于这项工作,DOM 和 SAX 哪个更好? (我的母语不是英语)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

各自安好 2024-12-26 03:18:11

您考虑过 XPath 吗?您的伪代码:

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

...直接转换为以下 XPath 表达式:

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

或者,更简洁(假设示例中的结构具有代表性):

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

有关在 Android 上使用 XPath 的信息可以在此处找到:

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:

邮友 2024-12-26 03:18:11

您可以使用 XmlPullParser 来执行此操作。请通过以下代码:

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

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");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文