Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用冲突标记解析java中的XML文件_Java_Xml - Fatal编程技术网

使用冲突标记解析java中的XML文件

使用冲突标记解析java中的XML文件,java,xml,Java,Xml,我有一个XML文件要用java解析,并在google map中使用该XML文件的元素。。 下面是我必须解析的xml <?xml version = '1.0' encoding = 'UTF-8'?> <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"> &l

我有一个XML文件要用java解析,并在google map中使用该XML文件的元素。。 下面是我必须解析的xml

      <?xml version = '1.0' encoding = 'UTF-8'?>
      <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
       <channel>
       <title/>
       <link/>
       <description/>
       <item>
        <title> hang </title>
        <description>Total: 291830</description>
         <geo:lat>52.0</geo:lat>
         <geo:long>-6.0</geo:long>
         <dc:subject>category1</dc:subject>
          </item>
          <item>
         <title> system </title>
         <description>Total: 78055</description>
        <geo:lat>36.66992187</geo:lat>
         <geo:long>48.97003173</geo:long>
        <dc:subject>category2</dc:subject>
        </item>
         <item>
          <title>chill </title>
         <description>Total: 43688</description>
         <geo:lat>2.0</geo:lat>
         <geo:long>3.8</geo:long>
         <dc:subject>category3</dc:subject>
          </item>
          </channel>
           </rss>

悬挂
总数:291830
52
-6.0
类别1
系统
总数:78055
36.66992187
48.97003173
类别2
寒冷
总数:43688
2
3.8
类别3

顶部的标题和说明与根节点项中的标题和说明冲突。有人能帮我解析xml并获取标题和说明的值吗???

当您要搜索/查询xml文件时,XPath往往是一个不错的选择,假设您使用的是DOM

try {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document dom = db.parse(new File("test.xml"));
    XPath xpath = XPathFactory.newInstance().newXPath();

    // Find the "thing" node...
    XPathExpression expession = xpath.compile("/rss/channel/item/title | /rss/channel/item/description");
    NodeList nl = (NodeList) expession.evaluate(dom, XPathConstants.NODESET);

    for (int index = 0; index < nl.getLength(); index++) {
        Node node = nl.item(index);
        System.out.println(node.getNodeName() + " = " + node.getTextContent());
    }

} catch (Exception exp) {
    exp.printStackTrace();
}

请查看以了解更多详细信息…

为什么这是一个冲突?这看起来像是应可解析的有效XML。它与位于顶部且位于根节点项内部的标题和说明标记混淆。我可以获取标题和说明的值,但由于冒号,我无法获取geo:lat的值…我如何获取该值?否,我以前从未尝试过…它对lat有效…Thak你太多了…:)
title =  hang 
description = Total: 291830
title =  system 
description = Total: 78055
title = chill 
description = Total: 43688