解析 XML 和检索信息 多层节点 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”标签中。我看过一些关于解析 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 <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>
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!
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以基本上使用 SAX 解析器对你来说是一个不错的选择(它速度快,允许你过滤掉所有不必要的数据,消耗低内存)。当第一次使用 SAX 时,您可能会发现以下示例很有用。我并不是说代码是完美的(它错过了异常处理、安全流关闭等),但它可能是您的一个很好的起点。
输出应如下所示:
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.
The output should look like this:
使用SAX并且只关注html_instructions标签。将为每个元素使用
startElement()
调用您的处理程序,并传入元素的名称。将该名称与“html_instructions”
进行比较。当您有匹配项时,请注意所有已处理的节点,直到相应的endElement()
调用。Use SAX and only pay attention to the html_instructions tag. Your handler will be called with
startElement()
for each element and is passed in the element's name. Compare that name to"html_instructions"
. When you have a match, pay attention to all processed nodes until the correspondingendElement()
call.