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