将String转换为XML时,doc元素返回null

时间:2017-11-07 11:24:55

标签: java xml string document

我有一个特定的字符串响应,如下所示。

<?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>

我需要将此String转换为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 >>>

我在这里犯的错误是什么?因为我的doc返回null。 感谢帮助。

由于

1 个答案:

答案 0 :(得分:0)

首先,您的xml包含名称空间,因此您需要指示DocumentBuilderFactory在创建Document时考虑名称空间。

factory.setNamespaceAware(true);

接下来,在选择节点时,您需要使用考虑名称空间的方法getElementsByTagNameNS

NodeList nodeList = doc.getElementsByTagNameNS("http://org.apache.axis2/xsd", "listUsersResponse");

最后,为了将xml Node的内容打印为String,您需要在Node上应用转换。

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();
    }