如何从包含XML的String创建Document?
我已尝试过以下代码,但 document.getElementsByTagNameNS(" Envelope"," http://schemas.xmlsoap.org/soap/envelope/") 函数调用找不到任何XML元素。
String xml = "<soapenv:Envelope\n" +
" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
"\t<soapenv:Header>\n" +
"\t</soapenv:Header>\n" +
"\t<soapenv:Body>\n" +
"\t</soapenv:Body>\n" +
"</soapenv:Envelope>";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
factory.setNamespaceAware(true);
builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(xml)));
System.out.println(document.getElementsByTagNameNS("Envelope", "http://schemas.xmlsoap.org/soap/envelope/").getLength());
} catch (Exception e) {
e.printStackTrace();
}
答案 0 :(得分:1)
根据文件,
NodeList getElementsByTagNameNS(String namespaceURI, String localName)
以下内容可行,
System.out.println(document.getElementsByTagNameNS("http://schemas.xmlsoap.org/soap/envelope/", "Envelope").getLength());