2013-08-29 40 views
0

我已經使用GWT的XMLParser來解析XML,但是當我嘗試解析屬性時會遇到麻煩。我可以從API文檔中看到,如果您知道屬性名稱將會提前(例如),那麼Element具有獲取Attribute節點或值的方法。你可以做GWT中的XML解析

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> 

有越來越屬性和列表的任何方式沒有硬編碼預期的屬性名稱?

回答