解析XML和放大器;检索节点深的Java / Android的信息几层 [英] Parse XML & Retrieve Info Several layers of Nodes Deep Java/Android

查看:126
本文介绍了解析XML和放大器;检索节点深的Java / Android的信息几层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从我的教授从天气预报的网站获取数据并解析XML文件显示在列表中的天气条件下提供了一个示例工作。我的计划是相似的,但我想找回嵌套多个节点内的信息,我不知道如何得到它。下面是XML文件,我从工作:

I am working from an example provided by my professor which gets data from a weather forecast site and parses the XML file to show the weather conditions in a list. My program is similar, but I want to retrieve information that is nested within several nodes, and I don't know how to get to it. Here is the XML file I'm working from:

<?xml version="1.0" encoding="UTF-8"?> 
<DirectionsResponse> 
 <status>OK</status> 
 <route> 
  <summary>S Street Viaduct</summary> 
  <leg> 
   <step> 
    <travel_mode>DRIVING</travel_mode> 
    <start_location> 
     <lat>40.7021400</lat> 
     <lng>-74.0158200</lng> 
    </start_location> 
    <end_location> 
     <lat>40.7021400</lat> 
     <lng>-74.0158200</lng> 
    </end_location> 
    <polyline> 
     <points>kslwFzewbM</points> 
     <levels>B</levels> 
    </polyline> 
    <duration> 
     <value>0</value> 
     <text>1 min</text> 
    </duration> 
    <html_instructions>Head &lt;b&gt;east&lt;/b&gt; on &lt;b&gt;S Street Viaduct&lt;/b&gt;</html_instructions> 
    <distance> 
     <value>0</value> 
     <text>1 ft</text> 
    </distance> 
   </step> 
   <duration> 
    <value>0</value> 
    <text>1 min</text> 
   </duration> 
   <distance> 
    <value>0</value> 
    <text>1 ft</text> 
   </distance> 
   <start_location> 
    <lat>40.7021400</lat> 
    <lng>-74.0158200</lng> 
   </start_location> 
   <end_location> 
    <lat>40.7021400</lat> 
    <lng>-74.0158200</lng> 
   </end_location> 
   <start_address>S Street Viaduct, New York, NY 10004, USA</start_address> 
   <end_address>S Street Viaduct, New York, NY 10004, USA</end_address> 
  </leg> 
  <copyrights>Map data ©2010 Google, Sanborn</copyrights> 
  <overview_polyline> 
   <points>kslwFzewbM</points> 
   <levels>B</levels> 
  </overview_polyline> 
 </route> 
</DirectionsResponse> 

我真的只关心检索的html_instructions标签中的信息,但它是嵌套在路径,腿,而步的标签。我见过几个教程和有关解析XML,但也似乎没有找到一个解决这个问题的SO。任何方向将是极大的AP preciated!

I'm really only interested in retrieving the info in the "html_instructions" tag, but it is nested in the "route", "leg", and "step" tags. I have seen several tutorials and questions on SO about parsing XML but couldn't seem to find a solution to this. Any direction would be greatly appreciated!

感谢。

推荐答案

所以基本上使用SAX解析器是你不错的选择(这是速度快,可以让你过滤掉所有不需要的数据,消耗低内存)。当使用SAX的第一次合作,你会发现下面的例子是有用的。我就不多说了code是完美的(它忽略如异常处理,安全流闭等),但它可能是你一个很好的起点。

So basically using the SAX parser is a good choice for you (it is fast, allows you to filter out all the unnecessary data, consumes low memory). When working with SAX for the first time, you may find the following example useful. I do not say the code is perfect (it misses e.g. exception handling, safe stream closing, etc.), but it could be a good start point for you.



import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Test {

  private static final String HTML_INSTRUCTIONS = "html_instructions";

  public static void main(String[] args) throws Exception {
    final List htmlInstructions = new ArrayList();

    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = spf.newSAXParser();
    DefaultHandler dh = new DefaultHandler() {
      private boolean isHtmlInstructions = false;
      private StringBuilder sb = new StringBuilder();
      @Override
      public void startElement(String uri, String localName, String name,
          Attributes attributes) throws SAXException {
        super.startElement(uri, localName, name, attributes);
        if (HTML_INSTRUCTIONS.equals(name)) {
          isHtmlInstructions = true;
        }
      }

      @Override
      public void characters(char ch[], int start, int length)
      throws SAXException {
        if (isHtmlInstructions) {
          sb.append(ch, start, length);
        }
      }

      @Override
      public void endElement(String uri, String localName, String name)
          throws SAXException {
        super.endElement(uri, localName, name);
        if (HTML_INSTRUCTIONS.equals(name)) {
          htmlInstructions.add(sb.toString());
          sb.delete(0, sb.length());
          isHtmlInstructions = false;
        }
      }
    };

    InputStream is = new FileInputStream("test.xml");
    sp.parse(is, dh);
    for (String htmlInstruction : htmlInstructions) {
      System.out.println(htmlInstruction);
    }

  }

}

输出应该是这样的:

The output should look like this:



Head <b>east on <b>S Street Viaduct</b>

这篇关于解析XML和放大器;检索节点深的Java / Android的信息几层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆