0

in my GWT web application, i am to retrieve XML data from the SOAP server. i haven't encountered any difficulties parsing this xml data in the client until i have this xml structure to parse:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <data>
    <list row="0">
      <product_name>A</product_name>
      <desc>product A</desc>
      <promo>
         &lt;?xml version="1.0" encoding="UTF-8"?&gt;
         &lt;root&gt;
           &lt;data/&gt;
         &lt;/root&gt;
      </promo>
    </list>
    <list row="1">
      <product_name>B</product_name>
      <desc>product B</desc>
      <promo>
         &lt;?xml version="1.0" encoding="UTF-8"?&gt;
         &lt;root&gt;
           &lt;data&gt;
              &lt;list row="0"&gt;
                 &lt;pname&gt;Test&lt;/pname&gt;
                 &lt;pdesc&gt;Test promo only&lt;/pdesc&gt;
              &lt;/list&gt;
           &lt;/data&gt;
         &lt;/root&gt;
      </promo>
    </list>    
  </data>
  <return_code>0</return_code>
</root>

having this xml, i have retrieved the product_name and desc of every list with NodeList and Element. but i also have to retrieve the promo of each product.

i have tried getting the promo by:

Document d = XMLParser.parse( xml ); // xml - xml data retrieved from server
Element element = d.getDocumentElement();
NodeList nlist = element.getElementsByTagName( "list" );
final int count = nlist.getLength();

for( int i = 0; i < count; ++i ) {
    final Element list = ( Element ) nlist.item( i );
    String product_name = ( (Text)list.getElementsByTagName( "fproductid" )
         .item(0).getFirstChild()).getData()
    ...

    // get promo
    String promo = d.getElementsByTagName( "promo" ).item(0).
         getFirstChild().getNodeValue();
    Document dpromo = XMLParser.parse( promo );
    ...
}

when i check what's the value of each promo, i have the same output:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <data/>
</root>

any idea how to get the fields/element inside the promo node (i.e. pname, pdesc)?

1 Answer 1

1

String promo = d.getElementsByTagName("promo").item(0).getFirstChild().getNodeValue();

You are looking at the same promo element in each iteration of the loop.

Replace d with list:

String promo = list.getElementsByTagName("promo").item(0).getFirstChild().getNodeValue();

and you will get the full text for each promo element.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.