Convert SOAP XML string to object or just read its contents










0















Am receiving a SOAP XML response from an endpoint and I am trying to read its' contents. I have tried a couple of methods but it keeps giving me null



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


My method 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));


None of the methods seem to work, they all give me null



My getString function



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;










share|improve this question

















  • 1





    Your first method using JAXB is sound - provided the JAXB Java class is the correct one for the given XML. You just need to feed the JAXB Unmarshaller just the relevant XML (the <CashOutPaymentRequestResult>...</CashOutPaymentRequestResult> part ) without the SOAP envelope. Use the SAAJ API to retrieve this and then unmarshall it. See docs.oracle.com/cd/E19644-01/817-5452/wsgjaxm.html for SAAJ.

    – Michal
    Nov 15 '18 at 16:04











  • Thanks @Michal was able to get it to work. Would post the implementation

    – alex davies
    Nov 15 '18 at 16:50















0















Am receiving a SOAP XML response from an endpoint and I am trying to read its' contents. I have tried a couple of methods but it keeps giving me null



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


My method 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));


None of the methods seem to work, they all give me null



My getString function



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;










share|improve this question

















  • 1





    Your first method using JAXB is sound - provided the JAXB Java class is the correct one for the given XML. You just need to feed the JAXB Unmarshaller just the relevant XML (the <CashOutPaymentRequestResult>...</CashOutPaymentRequestResult> part ) without the SOAP envelope. Use the SAAJ API to retrieve this and then unmarshall it. See docs.oracle.com/cd/E19644-01/817-5452/wsgjaxm.html for SAAJ.

    – Michal
    Nov 15 '18 at 16:04











  • Thanks @Michal was able to get it to work. Would post the implementation

    – alex davies
    Nov 15 '18 at 16:50













0












0








0








Am receiving a SOAP XML response from an endpoint and I am trying to read its' contents. I have tried a couple of methods but it keeps giving me null



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


My method 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));


None of the methods seem to work, they all give me null



My getString function



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;










share|improve this question














Am receiving a SOAP XML response from an endpoint and I am trying to read its' contents. I have tried a couple of methods but it keeps giving me null



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


My method 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));


None of the methods seem to work, they all give me null



My getString function



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;







java xml soap






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 10:16









alex daviesalex davies

11210




11210







  • 1





    Your first method using JAXB is sound - provided the JAXB Java class is the correct one for the given XML. You just need to feed the JAXB Unmarshaller just the relevant XML (the <CashOutPaymentRequestResult>...</CashOutPaymentRequestResult> part ) without the SOAP envelope. Use the SAAJ API to retrieve this and then unmarshall it. See docs.oracle.com/cd/E19644-01/817-5452/wsgjaxm.html for SAAJ.

    – Michal
    Nov 15 '18 at 16:04











  • Thanks @Michal was able to get it to work. Would post the implementation

    – alex davies
    Nov 15 '18 at 16:50












  • 1





    Your first method using JAXB is sound - provided the JAXB Java class is the correct one for the given XML. You just need to feed the JAXB Unmarshaller just the relevant XML (the <CashOutPaymentRequestResult>...</CashOutPaymentRequestResult> part ) without the SOAP envelope. Use the SAAJ API to retrieve this and then unmarshall it. See docs.oracle.com/cd/E19644-01/817-5452/wsgjaxm.html for SAAJ.

    – Michal
    Nov 15 '18 at 16:04











  • Thanks @Michal was able to get it to work. Would post the implementation

    – alex davies
    Nov 15 '18 at 16:50







1




1





Your first method using JAXB is sound - provided the JAXB Java class is the correct one for the given XML. You just need to feed the JAXB Unmarshaller just the relevant XML (the <CashOutPaymentRequestResult>...</CashOutPaymentRequestResult> part ) without the SOAP envelope. Use the SAAJ API to retrieve this and then unmarshall it. See docs.oracle.com/cd/E19644-01/817-5452/wsgjaxm.html for SAAJ.

– Michal
Nov 15 '18 at 16:04





Your first method using JAXB is sound - provided the JAXB Java class is the correct one for the given XML. You just need to feed the JAXB Unmarshaller just the relevant XML (the <CashOutPaymentRequestResult>...</CashOutPaymentRequestResult> part ) without the SOAP envelope. Use the SAAJ API to retrieve this and then unmarshall it. See docs.oracle.com/cd/E19644-01/817-5452/wsgjaxm.html for SAAJ.

– Michal
Nov 15 '18 at 16:04













Thanks @Michal was able to get it to work. Would post the implementation

– alex davies
Nov 15 '18 at 16:50





Thanks @Michal was able to get it to work. Would post the implementation

– alex davies
Nov 15 '18 at 16:50












1 Answer
1






active

oldest

votes


















0














Thanks to Michal's comment I developed a solution to this



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









share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317124%2fconvert-soap-xml-string-to-object-or-just-read-its-contents%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Thanks to Michal's comment I developed a solution to this



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









    share|improve this answer



























      0














      Thanks to Michal's comment I developed a solution to this



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









      share|improve this answer

























        0












        0








        0







        Thanks to Michal's comment I developed a solution to this



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









        share|improve this answer













        Thanks to Michal's comment I developed a solution to this



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










        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 17:09









        alex daviesalex davies

        11210




        11210





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317124%2fconvert-soap-xml-string-to-object-or-just-read-its-contents%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto