This discussion is archived
6 Replies Latest reply: Apr 9, 2013 8:15 AM by Tolls RSS

Need Help Parsing XML response

jl1997 Explorer
Currently Being Moderated
Hi, I am new to JAVA, and only work with it every once in a while.

I have a question which is probably very simple to someone with JAVA experience.

I have a piece of JAVA code written that will submit a credit card transaction request to a gateway, the gateways response come in the form of XML.

Below is the XML response file in its entirety.
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="https://www.domain.com/ws/">1,0,Transaction accepted,0</string>
I have worked with other XML responses and was able to get what I needed. However, this XML response differs a bit and I cannot figure out how to neatly parse out the string of text that I need at the end which is:
1,0,Transaction accepted,0
Any help would be appreciated.

Thanks!
  • 1. Re: Need Help Parsing XML response
    PhHein Guru Moderator
    Currently Being Moderated
    Hi and welcome,

    there are quite a few XML parsers around, like SAX. http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/
    If you Google you can find various examples and tutorials.
  • 2. Re: Need Help Parsing XML response
    jl1997 Explorer
    Currently Being Moderated
    Thanks for the link, I will explore it, but what I was trying to say before was I have been able to parse an XML file that has what I would consider a complete structure, but this XML has just 1 level of information, and I just have not been able to figure out yet how to parse it out.

    I will keep looking, thanks!
  • 3. Re: Need Help Parsing XML response
    jl1997 Explorer
    Currently Being Moderated
    I have used the following statements to try to get the information I need
      new PrintStream(ftemp).println("Root element :" + doc.getDocumentElement().getNodeName());
      new PrintStream(ftemp).println("Root value :" + doc.getDocumentElement().getAttribute("string"));
    Output from the above code is:
    Root element :string
    Root value :
    So I can identify the name of the node, which is "string", I just have not been able to fetch the contents of the node.

    Edited by: jl1997 on Apr 9, 2013 8:05 AM
  • 4. Re: Need Help Parsing XML response
    Tolls Journeyer
    Currently Being Moderated
    jl1997 wrote:
    I have used the following statements to try to get the information I need
    new PrintStream(ftemp).println("Root element :" + doc.getDocumentElement().getNodeName());
    new PrintStream(ftemp).println("Root value :" + doc.getDocumentElement().getAttribute("string"));
    Output from the above code is:
    Root element :string
    Root value :
    So I can identify the name of the node, which is "string", I just have not been able to fetch the contents of the node.

    Edited by: jl1997 on Apr 9, 2013 8:05 AM
    The <string> element has no attribute called "string". It only has content, which you get using getTextContent() or the equivalent for whatever you are using.
  • 5. Re: Need Help Parsing XML response
    jl1997 Explorer
    Currently Being Moderated
    PhHein,

    The link you referenced had a link to another site with information on the DOM parser which I had been using and I sort of stumbled onto what I needed. My question is now answered.

    Thank you for replying to my question!!!

    ".getTextContent()" gave me the value I was looking for:
                new PrintStream(ftemp).println("Root value :" + doc.getDocumentElement().getTextContent());
    Result is:
    Root value :1,0,Transaction accepted,0
  • 6. Re: Need Help Parsing XML response
    jl1997 Explorer
    Currently Being Moderated
    Tolls,

    "getTextContent()" is just what I needed.

    Thanks!

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points