Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.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从XML获取元素值_Java_Xml_Xpath_Xml Parsing - Fatal编程技术网

Java 使用XPath从XML获取元素值

Java 使用XPath从XML获取元素值,java,xml,xpath,xml-parsing,Java,Xml,Xpath,Xml Parsing,我有如下XML文件: <Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-

我有如下XML文件:

<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents- xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 UBL-Invoice-2.1.xsd">
<cac:AccountingSupplierParty>
<cac:Party>
  <cac:PartyIdentification>
    <cbc:ID schemeID="schema1">123231123</cbc:ID>
  </cac:PartyIdentification>
  <cac:PartyIdentification>
    <cbc:ID schemeID="schema2">2323232323</cbc:ID>
  </cac:PartyIdentification>
  <cac:PartyIdentification>
    <cbc:ID schemeID="schema3">4442424</cbc:ID>
  </cac:PartyIdentification>
  <cac:PostalAddress>
    <cbc:CityName>İstanbul</cbc:CityName>
    <cac:Country>
      <cbc:Name>Turkey</cbc:Name>
    </cac:Country>
  </cac:PostalAddress>
</cac:Party>
</cac:AccountingSupplierParty>
</Invoice>

binaryXmlData是我的XML的源。首先,我将Base64二进制数据转换为xml。转换错误还是xpath实现错误?

您的代码和XML存在许多问题,包括:

  • 您的XML格式不正确。
    cbc
    缺少命名空间前缀
  • Java代码从不定义
    名称空间上下文

  • 另请参见

    是否也在XPath表达式上设置了名称空间?
    try {
        String decoded = new 
        String(DatatypeConverter.parseBase64Binary(binaryXmlData));
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(decoded));
        Document doc = db.parse(is);
        String expression = "/Invoice/cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID@[schemaID='schema2']/text()";    
        String schema2 = (String) xPath.compile(expression).evaluate(doc, XPathConstants.STRING);
        System.out.println(schema2);
        //schema2 is null
    
        //Above this code block returns correct value
        NodeList nl = doc.getElementsByTagName("cbc:CityName");
        System.out.println(nl.item(0).getTextContent());
    
    } catch () {
    
    }