wonu wns
wonu wns

Reputation: 3608

Parsing XML in GWT with XMLParser

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

Upvotes: 0

Views: 4128

Answers (1)

funkybro
funkybro

Reputation: 8671

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.

Upvotes: 1

Related Questions