Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.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 如何在xpath中获取节点的值?_Java_Xml_Xpath - Fatal编程技术网

Java 如何在xpath中获取节点的值?

Java 如何在xpath中获取节点的值?,java,xml,xpath,Java,Xml,Xpath,我正在尝试获取LogicalId标记的值。以下是我的xml,它被编译成一个文档: <?xml version="1.0" encoding="utf-8"?> <ProcessCreditApplication xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <ApplicationArea> <Sender> <LogicalId>www.refij

我正在尝试获取LogicalId标记的值。以下是我的xml,它被编译成一个文档:

<?xml version="1.0" encoding="utf-8"?>
<ProcessCreditApplication xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ApplicationArea>
    <Sender>
      <LogicalId>www.refijet.com/ysg/defi_routeone</LogicalId>
...

www.refijet.com/ysg/defi_routeone
...
处理它的java代码如下所示:

        String expression = "//*[not(*)]";
        NodeList nl = (NodeList) xpath.compile(expression).evaluate(structure_xml, XPathConstants.NODESET);
        List<String> names = new ArrayList<>();
        for (int i = 0; i < nl.getLength(); i++) {
            names.add(nl.item(i).getNodeName());
            Node node = nl.item(i);
            String path = "";
            while (node != null) {
                path = node.getNodeName() + '/' + path;
                node = node.getParentNode();
            }

            //now get the value of the node located at path in the passed xml
            path = path.replace("#document/", "");  //get rid of the #document/ at the start of the path
            path = path.substring(0,path.length()-1);   //get rid of the / at the end to prevent illegal path
            System.out.println(path);
            expr = xpath.compile(path);
            Object result = expr.evaluate(xml, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;
            node = nodes.item(0);
            System.out.println("node value="+node.getNodeValue());
        }
String expression=“//*[非(*)]”;
NodeList nl=(NodeList)xpath.compile(expression).evaluate(structure_xml,XPathConstants.NODESET);
列表名称=新的ArrayList();
对于(int i=0;i
以下是路径的值:

ProcessCreditApplication/ApplicationArea/Sender/LogicalId

我也尝试过在前面加一个“/”和一个“/”,但结果是一样的。 以下是结果的值:

节点值=空


我做错了什么?

如果您只想获取LogicalId标记中的文本,那么XPath应该使用XPath表达式ProcessCreditApplication/ApplicationArea/Sender/LogicalId/text()获取其中的文本节点。还是你想做点别的?啊!我不知道text()。这很有效,谢谢你!很高兴这有帮助。干杯如果您只想获取LogicalId标记内的文本,那么XPath应该使用XPath表达式ProcessCreditApplication/ApplicationArea/Sender/LogicalId/text()获取其中的文本节点。还是你想做点别的?啊!我不知道text()。这很有效,谢谢你!很高兴这有帮助。干杯