Get XML node with namespace in java

前端 未结 3 1484
失恋的感觉
失恋的感觉 2021-01-21 15:43

I have the next XML:




        
相关标签:
3条回答
  • 2021-01-21 16:07

    A - Demo Code

    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 - Sample File : 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 - Sample Output

    ext:UBLExtension : Some Code
    ext:UBLExtension : What you want
    
    0 讨论(0)
  • 2021-01-21 16:09

    You need to make sure the XML parser is namespace aware.

    You should also make sure to close the FileInputStream, preferably using try-with-resources.

    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");
    
    0 讨论(0)
  • 2021-01-21 16:10

    You can try to work with xpath

    public static NodeList getNodesWithXPath(Node aNode, String aXPath) {
        try {
            XPathFactory xPathfactory = XPathFactory.newInstance();
            XPath xpath = xPathfactory.newXPath();
            XPathExpression xPathExpression = xpath.compile(aXPath);
            return (NodeList) xPathExpression.evaluate(aNode, XPathConstants.NODESET);
        } catch (XPathExpressionException e) {
            // ignore
        } catch (NullPointerException e) {
            // ignore
        }
        return null;
    }
    
    0 讨论(0)
提交回复
热议问题