如何从Java SOAP文档解析文本节点

时间:2019-03-28 21:31:00

标签: java xml dom soap

我有一个SOAP响应主体,在将SOAP主体提取到Document obj之后,我想从中解析所有文本节点。当我从ELEMENT_NODEs尝试getTextContent()时,它将返回父节点下的所有子文本,而当我尝试从TEXT_NODES中获取文本时,则什么也不返回。

SOAPMessage soapResponse = soapConnection.call(soapMessage, url);
soapConnection.close();

return representXMLasStringArray(soapResponse.getSOAPBody().extractContentAsDocument());

private static ArrayList<String> representXMLasStringArray(Document doc) {
    ArrayList<String>nodeList=new ArrayList<String>();

    try {
        //doc.getDocumentElement().normalize();
        NodeList nodes = doc.getElementsByTagName("*");

        for(int i =0;i<nodes.getLength() ;i++) {
            Node node = nodes.item(i);
            if (node.getNodeType() == Node.TEXT_NODE) {//also tried ELEMENT_NODE
                nodeList.add(node.getNodeName()+": "+node.getTextContent());
            }
        }       
    }
    catch(Exception e) {
        e.printStackTrace();
    }
    for(String s:nodeList) {
        System.out.println(s); //prints nothing
    }

    return nodeList;
}

0 个答案:

没有答案
相关问题