0

GWT の XMLParser を使用して XML を解析しましたが、属性を解析しようとすると問題が発生します。API ドキュメントから、属性名が事前にわかっている場合、要素には属性ノードまたは値を取得するメソッドがあることがわかります。たとえば、次のことができます。

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>

予想される属性名をハードコーディングせずに、属性とその値のリストを取得する方法はありますか?

4

1 に答える 1

2

http://www.gwtproject.org/javadoc/latest/com/google/gwt/xml/client/Node.html#getAttributes()で可能になるはずだと思います

于 2013-08-29T20:55:42.640 に答える