Java 如何在android中解析xml的内部标记
这是我的xml。请告诉我DOM解析方法来解析整个视频标签。实际上,它有一个位置的内部标签,这让我很不安Java 如何在android中解析xml的内部标记,java,android,Java,Android,这是我的xml。请告诉我DOM解析方法来解析整个视频标签。实际上,它有一个位置的内部标签,这让我很不安 <video> <video_id>39</video_id> <title>test video</title> <description>asdasd asd as</description> <address/> <location>
<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>
39
测试视频
asdasd asd as
-86.785012400000030
33.353920000000000
2055555555
http://www.google.com
154
http://www.youtube.com/watch?v=sdfgd
半干法
用户内容
48
本教程可能会对您有所帮助
下面是一个示例,将xml读入
文档
对象,然后使用xpath
计算经度节点的文本内容。确保video.xml
位于应用程序的类路径中(将其放在与java相同的目录中)
我只添加xpath作为关注点,向您展示如何查询返回的文档
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());
}
}
}
公共类ReadXML{
私有静态最终文档builderFactory文档\u BUILDER\u FACTORY=DocumentBuilderFactory.newInstance();
私有静态最终XPathFactory XPATH_FACTORY=XPathFactory.newInstance();
公共静态void main(字符串[]args)引发异常{
新建ReadXML().parseXml();
}
私有void parseXml()引发异常{
DocumentBuilder db=DOCUMENT_BUILDER_FACTORY.newDocumentBuilder();
InputStream=this.getClass().getResourceAsStream(“video.xml”);
最终文档=db.parse(流);
XPath xPathEvaluator=XPath_FACTORY.newXPath();
XPathExpression expr=xpatheevaluator.compile(“视频/位置/经度”);
NodeList节点=(NodeList)expr.evaluate(文档,XPathConstants.NODESET);
对于(int i=0;i
只是一个建议,但由于这是针对移动平台的,我认为您应该使用内存消耗较少的解析器,比如SAXIT,它很好。。但是我需要iner标记的代码。就像在这个xml中,位置就是iner标记。请告诉我如何解析这个iner标记(int i=0;i