Get XML node with namespace in java

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

I have the next XML:




        
3条回答
  •  误落风尘
    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");
    

提交回复
热议问题