我已经使用GWT的XMLParser来解析XML,但是当我尝试解析属性时,麻烦就来了。我可以从 API 文档中看到,如果您提前知道属性名称是什么,则 Element 有获取属性节点或值的方法,例如你可以做
element.getAttribute("name");
但是没有方法可以检索所有属性。
所以我尝试了这种方法:
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!
}
}
找不到任何属性的 XML 响应如下:
<?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>
是否有任何方法可以获取属性及其值的列表,而无需对预期的属性名称进行硬编码?
最佳答案
我认为 http://www.gwtproject.org/javadoc/latest/com/google/gwt/xml/client/Node.html#getAttributes() 应该可以
关于java - GWT 中的 XML 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18520253/