0

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?

1 Answer 1

2

I think it should be possible with http://www.gwtproject.org/javadoc/latest/com/google/gwt/xml/client/Node.html#getAttributes()

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.