[英]Parse XML & Retrieve Info Several layers of Nodes Deep Java/Android
我正在從教授提供的示例中進行工作,該示例從天氣預報站點獲取數據並解析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 <b>east</b> on <b>S Street Viaduct</b></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”標記中。 我已經看過關於SO解析XML的一些教程和問題,但似乎找不到解決方案。 任何方向將不勝感激!
謝謝。
因此,基本上,使用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>
使用SAX,僅注意html_instructions標記。 您的處理程序將使用每個元素的startElement()
調用,並以元素的名稱傳遞。 將該名稱與"html_instructions"
進行比較。 當您有一個匹配項時,請注意所有已處理的節點,直到調用了相應的endElement()
為止。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.