在java中获取带有命名空间的XML节点

时间:2016-07-11 16:10:40

标签: java xml

我有下一个XML:

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<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-2"
         xmlns:ccts="urn:un:unece:uncefact:documentation:2"
         xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
         xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
         xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2"
         xmlns:sac="urn:sunat:names:specification:ubl:peru:schema:xsd:SunatAggregateComponents-1"
         xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ext:UBLExtensions>
        <ext:UBLExtension>
            <ext:ExtensionContent>
                <!-- SOME CODE HERE -->
            </ext:ExtensionContent>
        </ext:UBLExtension>
        <ext:UBLExtension>
            <ext:ExtensionContent>
                <!-- I WANT TO GET THIS NODE -->
            </ext:ExtensionContent>
        </ext:UBLExtension>
    </ext:UBLExtensions>

如何获得此节点?我试过了

Document doc = dbf.newDocumentBuilder().parse(new FileInputStream(PATH_TO_MY_XML));
NodeList nodes = doc.getDocumentElement().getElementsByTagNameNS("*", "UBLExtension");

但它返回一个空数组。我该怎么办?

3 个答案:

答案 0 :(得分:3)

A - 演示代码

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class GetXMLNodeInJavaDemo {

    public static void main(String[] args) {

        try {
            File fXmlFile = new File("sampleFile.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

            dbFactory.setNamespaceAware(true);

            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);

            doc.getDocumentElement().normalize();

            printByElementTagname(doc);

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

    private static void printByElementTagname(Document doc) {
        NodeList nodes = doc.getDocumentElement().getElementsByTagNameNS("*", "UBLExtension");
        Node node;
        for(int i = 0; i < nodes.getLength(); i++) {
            node = nodes.item(i);
            System.out.println(node.getNodeName() + " : " + node.getTextContent().trim());
        }
    }

}

B - 示例文件:sampleFile.xml

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<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-2"
         xmlns:ccts="urn:un:unece:uncefact:documentation:2"
         xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
         xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
         xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2"
         xmlns:sac="urn:sunat:names:specification:ubl:peru:schema:xsd:SunatAggregateComponents-1"
         xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ext:UBLExtensions>
        <ext:UBLExtension>
            <ext:ExtensionContent>
                Some Code
            </ext:ExtensionContent>
        </ext:UBLExtension>
        <ext:UBLExtension>
            <ext:ExtensionContent>
                What you want
            </ext:ExtensionContent>
        </ext:UBLExtension>
    </ext:UBLExtensions>
</Invoice>

C - 样本输出

ext:UBLExtension : Some Code
ext:UBLExtension : What you want

答案 1 :(得分:1)

您可以尝试使用xpath

FileInputStream

答案 2 :(得分:1)

您需要确保XML解析器可识别名称空间。

您还应确保关闭DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); Document doc; try (InputStream xmlStream = new FileInputStream(PATH_TO_MY_XML)) { doc = dbf.newDocumentBuilder().parse(xmlStream); } NodeList nodes = doc.getDocumentElement().getElementsByTagNameNS("*", "UBLExtension"); ,最好使用try-with-resources。

#calendar