簡體   English   中英

XML - 理解使用JDOM解析XML的困難

[英]XML - Understanding difficulties parsing XML using JDOM

我得到了這個特殊的XML文件,它看起來像這樣:

<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-Method給出了這個輸出:App01_Storage [[Attribute:Type =“VOLATILE”],[Attribute:Size =“200000”],[Attribute:Speed =“15”],[Attribute:LatencyMaxWrite =“1 “],[屬性:LatencyMaxRead =”2“],[屬性:Endurance =”12“],[屬性:WorkloadPeak =”15“]]

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

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

我看錯了嗎? 希望我能解釋一下自己。 非常感謝:)

node.getAttributes()為您提供了可以重復迭代的屬性列表。 您看到的輸出只是列表toString()方法的結果,當您將其交給System.out.println()時會調用它。

做這樣的事情:

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

還有其他已經返回特定類型的get方法(如getIntValue() )。 查看Attribute文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM