Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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 如何在nodelist中检索属性值_Java_Xml_Dom_Xpath_Sax - Fatal编程技术网

Java 如何在nodelist中检索属性值

Java 如何在nodelist中检索属性值,java,xml,dom,xpath,sax,Java,Xml,Dom,Xpath,Sax,对于下面的xml文件,我想检索对应于lat=53.0337395的ID的值,在xml中有两个lat=53.0337395的ID。如下图所示,为了实现这一点,我编写了下面的代码,但在运行时,我收到的编号不能转换为节点列表 请让我知道如何解决它 String expr0 = "count(//node[@lat=53.0337395]//@id)"; xPath.compile(expr0); NodeList nodeList = (NodeList) xPath.compile(expr0).e

对于下面的xml文件,我想检索对应于lat=53.0337395的ID的值,在xml中有两个lat=53.0337395的ID。如下图所示,为了实现这一点,我编写了下面的代码,但在运行时,我收到的编号不能转换为节点列表

请让我知道如何解决它

String expr0 = "count(//node[@lat=53.0337395]//@id)";
xPath.compile(expr0);
NodeList nodeList = (NodeList) xPath.compile(expr0).evaluate(document, 
XPathConstants.NODESET);
System.out.println(nodeList.getLength());
xml

<?xml version='1.0' encoding='utf-8' ?>
<osm>
<node id="25779111" lat="53.0334062" lon="8.8461545"/>
<node id="25779112" lat="53.0338904" lon="8.846314"/>
<node id="25779119" lat="53.0337395" lon="8.8489255"/>
<tag k="maxspeed" v="30"/>  
<tag k="maxspeed:zone" v="yes"/>
<node id="25779111" lat="53x.0334062" lon="8x.8461545"/>
<node id="25779112" lat="53x.0338904" lon="8x.846314"/>
<node id="257791191" lat="53.0337395" lon="8x.8489255"/>
<tag k="maxspeed" v="30x"/> 
<tag k="maxspeed:zone" v="yes"/>
</osm>

如果要获取节点列表(
count()
将返回一个数字,而不是列表),我不确定为什么要使用
count()
)。请尝试以下方法:

String expr0 = "/osm/node[@lat=53.0337395]/@id";
NodeList nodeList = (NodeList) xPath.compile(expr0).evaluate(document,
                                                             XPathConstants.NODESET);
System.out.println(nodeList.getLength());
以下是使用XML文件作为输入的完整可编译示例:

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class IdFinder
{
    public static void main(String[] args)
            throws Exception
    {
        File fXmlFile = new File("C:/Users/user2121/osm.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document document = dBuilder.parse(fXmlFile);

        XPath xPath = XPathFactory.newInstance().newXPath();

        String expr0 = "/osm/node[@lat=53.0337395]/@id";
        NodeList nodeList = (NodeList) xPath.compile(expr0).evaluate(document, XPathConstants.NODESET);

        System.out.println("Matches: " + nodeList.getLength());
        for (int i = 0; i < nodeList.getLength(); i++) {
            System.out.println(nodeList.item(i).getNodeValue());
        }
    }
}
导入java.io.File;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.xpath.xpath;
导入javax.xml.xpath.XPathConstants;
导入javax.xml.xpath.XPathFactory;
导入org.w3c.dom.Document;
导入org.w3c.dom.NodeList;
公共类IdFinder
{
公共静态void main(字符串[]args)
抛出异常
{
File fXmlFile=新文件(“C:/Users/user2121/osm.xml”);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
documentdocument=dBuilder.parse(fXmlFile);
XPath=XPathFactory.newInstance().newXPath();
String expr0=“/osm/node[@lat=53.0337395]/@id”;
NodeList NodeList=(NodeList)xPath.compile(expr0).evaluate(文档,XPathConstants.NODESET);
System.out.println(“匹配:“+nodeList.getLength());
for(int i=0;i
其输出为:

Matches: 2 25779119 257791191 比赛:2场 25779119 257791191
String expr0=“count(//node[@lat=53.0337395]//@id)”
在您的例子中应该返回2,您的意思是2应该是节点列表