这是我的xml。请告诉我解析整个视频标签的DOM解析方法。实际上它有一个位置的内部标记令我感到不安。
<video>
<video_id>39</video_id>
<title>test video</title>
<description>asdasd asd as</description>
<address/>
<location>
<longitude>-86.785012400000030</longitude>
<latitude>33.353920000000000</latitude>
</location>
<phone>2055555555</phone>
<website>http://www.google.com</website>
<time>154</time>
<youtube_url>http://www.youtube.com/watch?v=sdfgd</youtube_url>
<youtube_video_id>sdfgd</youtube_video_id>
<category_name>User Content</category_name>
<category_id>48</category_id>
</video>
答案 0 :(得分:2)
答案 1 :(得分:0)
以下是将xml读入Document
对象,然后使用xpath
评估经度节点的文本内容的示例。确保video.xml
位于app的类路径中(将其放在与java相同的目录中)。
我只添加xpath作为兴趣点,向您展示如何查询返回的Document
。
public class ReadXML {
private static final DocumentBuilderFactory DOCUMENT_BUILDER_FACTORY = DocumentBuilderFactory.newInstance();
private static final XPathFactory XPATH_FACTORY = XPathFactory.newInstance();
public static void main(String[] args) throws Exception {
new ReadXML().parseXml();
}
private void parseXml() throws Exception {
DocumentBuilder db = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder();
InputStream stream = this.getClass().getResourceAsStream("video.xml");
final Document document = db.parse(stream);
XPath xPathEvaluator = XPATH_FACTORY.newXPath();
XPathExpression expr = xPathEvaluator.compile("video/location/longitude");
NodeList nodes = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
Node item = nodes.item(i);
System.out.println(item.getTextContent());
}
}
}