2013-08-16 61 views
0

XML理解困難,我得到了這個特定的XML文件,它看起來像這樣:XML - 解析使用JDOM

<App01_Storage Type="VOLATILE" Size="200000" Speed="15" LatencyMaxWrite="1" LatencyMaxRead="2" Endurance="12" WorkloadPeak="15" /> 

在我的節目,我通過根節點的所有兒童進行迭代。我的意圖是讓所有的孩子都擁有屬性+值。一個孩子看起來像上面的代碼。

System.out.println(node.getName()); 
System.out.println(node.getAttributes()); 

的System.out的法給了我這樣的輸出: App01_Storage [屬性:TYPE = 「揮發性」] [屬性:尺寸= 「200000」] [屬性:速度= 「15」 ] [屬性:LatencyMaxWrite =「1」],[Attribute:LatencyMaxRead =「2」],[Attribute:Endurance =「12」],[Attribute:WorkloadPeak =「15」]]

我想我是在正確的方式。根據我的理解,一個屬性應該看起來像這樣:Attribute.Name = Attribute.Value

我想將屬性加上值保存在不同的類中,並且不知道如何確切地獲得值和名稱separetaly。我現在得到的輸出是一個List,每個條目Attributename = Attributevalue,就像一個String。有了這個單一的字符串,我無法工作。

我看到有什麼問題嗎?希望我能解釋一下自己。非常感謝:)

回答

2

node.getAttributes()給你一個屬性列表,你可以再次迭代。您看到的輸出只是toString()方法的結果,當您將其交給System.out.println()時,會調用該方法。

做這樣的事情:

for(Attribute attribute : node.getAttributes()) { 
    String attributeName = attribute.getName(); 
    String value = attribute.getValue(); 
} 

也有一些已經返回某一類型(如getIntValue())等獲得的方法。看看Attribute documentation