Преобразовать строку SOAP XML в объект или просто прочитать ее содержимое - PullRequest
0 голосов
/ 15 ноября 2018

Я получаю ответ 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 " ";
}

My get 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 голосов
/ 15 ноября 2018

Благодаря комментарию Михала я разработал решение для этого

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")) {

                }
            }
        }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...