0

我正在使用我的教授提供的示例,该示例从天气预报站点获取数据并解析 XML 文件以在列表中显示天气状况。我的程序类似,但我想检索嵌套在多个节点中的信息,但我不知道如何获取它。这是我正在使用的 XML 文件:

<?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”标签中的信息感兴趣,但它嵌套在“route”、“leg”和“step”标签中。我已经看过几个关于解析 XML 的教程和问题,但似乎找不到解决方案。任何方向将不胜感激!

谢谢。

4

2 回答 2

4

所以基本上使用 SAX 解析器对你来说是一个不错的选择(它速度很快,可以让你过滤掉所有不必要的数据,消耗低内存)。第一次使用 SAX 时,您可能会发现以下示例很有用。我并不是说代码是完美的(它错过了例如异常处理、安全流关闭等),但它可能是您的一个很好的起点。


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

  }

}

输出应如下所示:


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

于 2010-12-10T22:52:55.557 回答
3

使用 SAX 并且只注意 html_instructions 标记。将为每个元素调用您的处理程序startElement(),并传入元素的名称。将该名称与"html_instructions". 当您有匹配时,请注意所有已处理的节点,直到相应的endElement()调用。

于 2010-12-10T21:47:50.713 回答