当将 String 转换为 XML 时,doc 元素 returns 为 null
doc element returns null when convert String to XML
我有如下特定的字符串响应。
<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns:listUsersResponse xmlns:ns="http://org.apache.axis2/xsd" xmlns:ax2754="http://common.mgt.user.carbon.wso2.org/xsd"><ns:return>admin</ns:return><ns:return>admin@wso2.com</ns:return><ns:return>is530@wso2.com</ns:return><ns:return>kavitha@gmail.com</ns:return><ns:return>normal1@gmail.com</ns:return><ns:return>normal2@gmail.com</ns:return><ns:return>normal3@gmail.com</ns:return><ns:return>sales1@gmail.com</ns:return><ns:return>sales2@gmail.com</ns:return><ns:return>sales3@gmail.com</ns:return><ns:return>sales4@gmail.com</ns:return><ns:return>sales5@gmail.com</ns:return><ns:return>sales6@gmail.com</ns:return><ns:return>salesf530@gmail.com</ns:return><ns:return>sf530@gmail.com</ns:return><ns:return>user1</ns:return><ns:return>user1234</ns:return><ns:return>user1@wso2.com</ns:return><ns:return>user2</ns:return><ns:return>user2@wso2.com</ns:return><ns:return>user@wso2.com</ns:return><ns:return>user@wso2.com</ns:return><ns:return>ushani01</ns:return></ns:listUsersResponse></soapenv:Body></soapenv:Envelope>
我需要将此字符串转换为 XML。
下面是我用来做的代码。
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try
{
builder = factory.newDocumentBuilder();
Document doc = builder.parse( new InputSource( new StringReader(outputString) ) );
System.out.println(" doc >>>");
System.out.println(doc);
NodeList nodeList = doc.getElementsByTagName("ns:listUsersResponse xmlns:ns=\"http://org.apache.axis2/xsd\" xmlns:ax2754=\"http://common.mgt.user.carbon.wso2.org/xsd");
System.out.println(" nodeList >>>");
System.out.println(nodeList);
if (nodeList.getLength() > 0) {
Element element = (Element)nodeList.item(0);
System.out.println(element.getElementsByTagName("ns:return")
.item(0).getTextContent());
} else {
System.out.println(" crazy >>>");
}
} catch (Exception e) {
e.printStackTrace();
}
下面是我的输出。
outputString >>>
<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns:listUsersResponse xmlns:ns="http://org.apache.axis2/xsd" xmlns:ax2754="http://common.mgt.user.carbon.wso2.org/xsd"><ns:return>admin</ns:return><ns:return>admin@wso2.com</ns:return><ns:return>is530@wso2.com</ns:return><ns:return>kavitha@gmail.com</ns:return><ns:return>normal1@gmail.com</ns:return><ns:return>normal2@gmail.com</ns:return><ns:return>normal3@gmail.com</ns:return><ns:return>sales1@gmail.com</ns:return><ns:return>sales2@gmail.com</ns:return><ns:return>sales3@gmail.com</ns:return><ns:return>sales4@gmail.com</ns:return><ns:return>sales5@gmail.com</ns:return><ns:return>sales6@gmail.com</ns:return><ns:return>salesf530@gmail.com</ns:return><ns:return>sf530@gmail.com</ns:return><ns:return>user1</ns:return><ns:return>user1234</ns:return><ns:return>user1@wso2.com</ns:return><ns:return>user2</ns:return><ns:return>user2@wso2.com</ns:return><ns:return>user@wso2.com</ns:return><ns:return>user@wso2.com</ns:return><ns:return>ushani01</ns:return></ns:listUsersResponse></soapenv:Body></soapenv:Envelope>
doc >>>
[#document: null]
nodeList >>>
org.apache.xerces.dom.DeepNodeListImpl@4c1eac3a
crazy >>>
我在这里东东是什么错误?因为我的文档 returns 为空。
感谢帮助。
谢谢
首先,您的 xml 包含命名空间,因此您需要指示 DocumentBuilderFactory
在创建 Document
.
时考虑命名空间
factory.setNamespaceAware(true);
接下来,在选择节点时,您需要使用方法 getElementsByTagNameNS
,该方法将名称空间考虑在内。
NodeList nodeList = doc.getElementsByTagNameNS("http://org.apache.axis2/xsd", "listUsersResponse");
最后,为了将 xml Node
的内容打印为 String
,您需要在 Node
.[=19 上应用转换=]
System.out.println(nodeToString(doc));
private static String nodeToString(Node node) throws TransformerConfigurationException, TransformerException {
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
StringWriter buffer = new StringWriter();
transformer.transform(new DOMSource(node), new StreamResult(buffer));
return buffer.toString();
}
我有如下特定的字符串响应。
<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns:listUsersResponse xmlns:ns="http://org.apache.axis2/xsd" xmlns:ax2754="http://common.mgt.user.carbon.wso2.org/xsd"><ns:return>admin</ns:return><ns:return>admin@wso2.com</ns:return><ns:return>is530@wso2.com</ns:return><ns:return>kavitha@gmail.com</ns:return><ns:return>normal1@gmail.com</ns:return><ns:return>normal2@gmail.com</ns:return><ns:return>normal3@gmail.com</ns:return><ns:return>sales1@gmail.com</ns:return><ns:return>sales2@gmail.com</ns:return><ns:return>sales3@gmail.com</ns:return><ns:return>sales4@gmail.com</ns:return><ns:return>sales5@gmail.com</ns:return><ns:return>sales6@gmail.com</ns:return><ns:return>salesf530@gmail.com</ns:return><ns:return>sf530@gmail.com</ns:return><ns:return>user1</ns:return><ns:return>user1234</ns:return><ns:return>user1@wso2.com</ns:return><ns:return>user2</ns:return><ns:return>user2@wso2.com</ns:return><ns:return>user@wso2.com</ns:return><ns:return>user@wso2.com</ns:return><ns:return>ushani01</ns:return></ns:listUsersResponse></soapenv:Body></soapenv:Envelope>
我需要将此字符串转换为 XML。
下面是我用来做的代码。
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try
{
builder = factory.newDocumentBuilder();
Document doc = builder.parse( new InputSource( new StringReader(outputString) ) );
System.out.println(" doc >>>");
System.out.println(doc);
NodeList nodeList = doc.getElementsByTagName("ns:listUsersResponse xmlns:ns=\"http://org.apache.axis2/xsd\" xmlns:ax2754=\"http://common.mgt.user.carbon.wso2.org/xsd");
System.out.println(" nodeList >>>");
System.out.println(nodeList);
if (nodeList.getLength() > 0) {
Element element = (Element)nodeList.item(0);
System.out.println(element.getElementsByTagName("ns:return")
.item(0).getTextContent());
} else {
System.out.println(" crazy >>>");
}
} catch (Exception e) {
e.printStackTrace();
}
下面是我的输出。
outputString >>>
<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns:listUsersResponse xmlns:ns="http://org.apache.axis2/xsd" xmlns:ax2754="http://common.mgt.user.carbon.wso2.org/xsd"><ns:return>admin</ns:return><ns:return>admin@wso2.com</ns:return><ns:return>is530@wso2.com</ns:return><ns:return>kavitha@gmail.com</ns:return><ns:return>normal1@gmail.com</ns:return><ns:return>normal2@gmail.com</ns:return><ns:return>normal3@gmail.com</ns:return><ns:return>sales1@gmail.com</ns:return><ns:return>sales2@gmail.com</ns:return><ns:return>sales3@gmail.com</ns:return><ns:return>sales4@gmail.com</ns:return><ns:return>sales5@gmail.com</ns:return><ns:return>sales6@gmail.com</ns:return><ns:return>salesf530@gmail.com</ns:return><ns:return>sf530@gmail.com</ns:return><ns:return>user1</ns:return><ns:return>user1234</ns:return><ns:return>user1@wso2.com</ns:return><ns:return>user2</ns:return><ns:return>user2@wso2.com</ns:return><ns:return>user@wso2.com</ns:return><ns:return>user@wso2.com</ns:return><ns:return>ushani01</ns:return></ns:listUsersResponse></soapenv:Body></soapenv:Envelope>
doc >>>
[#document: null]
nodeList >>>
org.apache.xerces.dom.DeepNodeListImpl@4c1eac3a
crazy >>>
我在这里东东是什么错误?因为我的文档 returns 为空。 感谢帮助。
谢谢
首先,您的 xml 包含命名空间,因此您需要指示 DocumentBuilderFactory
在创建 Document
.
factory.setNamespaceAware(true);
接下来,在选择节点时,您需要使用方法 getElementsByTagNameNS
,该方法将名称空间考虑在内。
NodeList nodeList = doc.getElementsByTagNameNS("http://org.apache.axis2/xsd", "listUsersResponse");
最后,为了将 xml Node
的内容打印为 String
,您需要在 Node
.[=19 上应用转换=]
System.out.println(nodeToString(doc));
private static String nodeToString(Node node) throws TransformerConfigurationException, TransformerException {
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
StringWriter buffer = new StringWriter();
transformer.transform(new DOMSource(node), new StreamResult(buffer));
return buffer.toString();
}