将SOAP XML字符串转换为对象或仅读取其内容

时间:2018-11-15 10:16:58

标签: java xml soap

我正在从端点接收SOAP XML响应,而我正在尝试读取其内容。我已经尝试了几种方法,但是它总是让我为空

String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><CashOutPaymentRequestResponse xmlns=\"http://tempuri.org/\"><CashOutPaymentRequestResult><ResponseCode>1002</ResponseCode><ResponseDesc>FAC Code and Amount does not match, Contact your Admin.</ResponseDesc></CashOutPaymentRequestResult></CashOutPaymentRequestResponse></soap:Body></soap:Envelope>";

JAXBContext jaxbContext = JAXBContext.newInstance(cashOutPaymentRequestEnvelope.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

StringReader reader = new StringReader(xml);
cashOutPaymentRequestEnvelope person = (cashOutPaymentRequestEnvelope) unmarshaller.unmarshal(reader);

我的方法2

Element rootdecryptedXml = XmlParseHelper.getStringXml(xml);
String responseUnEscapedXml = StringEscapeUtils.unescapeXml(XmlParseHelper.getString("CashOutPaymentRequestResult", rootdecryptedXml));

String responseEscapedXml = XmlParseHelper.getString("CashOutPaymentRequestResult", rootdecryptedXml);

   System.out.println("step 1.. \n" + responseEscapedXml);

   Element root = XmlParseHelper.getStringXml(responseEscapedXml);

   System.out.println("step 2aa...\n" + XmlParseHelper.getString("ResponseCode", root));
   System.out.println("step 2bb...\n" + XmlParseHelper.getString("ResponseDesc", root));

这些方法似乎都不起作用,它们全都给了我空值

我的getString函数

public static String getString(String tagName, Element element) {
    if (element == null) {
        return " ";
    }

    NodeList list = element.getElementsByTagName(tagName);

    if (list != null && list.getLength() > 0) {
        NodeList subList = list.item(0).getChildNodes();
        if (subList != null && subList.getLength() > 0) {
            return subList.item(0).getNodeValue();
        }
    }
    return " ";
}

我得到StringXml

public static Element getStringXml(String responseXml) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document document;
    Element rootdecryptedXml = null;
    DocumentBuilder builder;
    builder = factory.newDocumentBuilder();
    document = builder.parse(new InputSource(new StringReader(responseXml)));
    rootdecryptedXml = document.getDocumentElement();
    return rootdecryptedXml;
}

1 个答案:

答案 0 :(得分:0)

感谢Michal的评论,我为此找到了解决方案

InputStream is = new ByteArrayInputStream(xml.getBytes());
        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage message = factory.createMessage(null, is);
        NodeList listResult = message.getSOAPBody().getElementsByTagName("CashOutPaymentRequestResult");
        for (int i = 0; i < listResult.getLength(); i++) {
            NodeList children = listResult.item(i).getChildNodes();
            for (int k = 0; k < children.getLength(); k++) {
                System.out.println(children.item(k).getNodeName());
                System.out.println(children.item(k).getTextContent());
                if (children.item(k).getNodeName().equals("ResponseCode")) {

                }

                if (children.item(k).getNodeName().equals("ResponseDesc")) {

                }
            }
        }