NickJ
NickJ

Reputation: 9559

XML Parsing in GWT

I have used GWT's XMLParser to parse XML, but the trouble comes when I try to parse attributes. I can see from the API docs that Element has methods to get Attribute nodes or values if you know what the attribute names are going to be in advance, e.g. you can do

element.getAttribute("name");

But there's no method for retrieving all attributes.

So I tried this way:

import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.Attr;
...
NodeList nodes = element.getChildNodes();
for (int i=0; i<nodes.getLength(); i++) {
    Node node = nodes.item(i);
    if (node instanceof Element) {
        //do something with child element
    }
    if (node instanceof Text) {
        //do something with text
    }
    if (node instanceof Attr) {
        //this is never reached!
    }
}

The XML response which it fails to find any attributes is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<grid>
  <field primary="true" id="volunteerId" caption="ID" width="30" type="integer"/>
  <field id="name" caption="Name" filter="true" type="concat">
    <field id="forename"/>
    <field id="surname" />
  </field>
  <field id="role" caption="Role" filter="true" type="text"/>
  <field id="instructions" caption="Instructions" type="boolean"/>
  <field id="security" caption="SIA" type="boolean" image="security"/>
</grid>

Is there any way of getting a list of attributes and their values without hard-coding the expected attribute names?

Upvotes: 0

Views: 892

Answers (1)

Related Questions