dom4j.Node處理xml文件,並且我在Map類似的結構中遇到了問題。 我按照文檔結構:dom4j.Node元素的迭代映射
<Party>
<Identifier>1113ddbed7b54890abfe2f8c9754d689</Identifier>
<Address>
</Address>
<Contact>
<ContactInfo>
<Key>IPS</Key>
<Value>null</Value>
<Key>keyTwo</Key>
<Value>1234</Value>
(...)
</ContactInfo>
</Contact>
</Party>
而我的目標是爲keyTwo元獲得價值。如何用靈活的非硬編碼方式獲得這些元素?
我的第一個想法是類似的東西:
//parent node is father element
Node parentDocument = parentNode.selectSingleNode("ContactInfo");
List<Node> nodesKeys = contactNode.selectNodes("Key");
List<Node> nodesValues = contactNode.selectNodes("Value");
for(int i=0; i<nodesKeys.size(); i++){
if(nodesKeys.get(i).selectSingleNode("Key").equals("keyTwo")){
return nodesValues.get(i);
}
}
但我不知道這是好辦法,特別是如果鍵和值的列表將被正確排序。
什麼的大小xml文件? – 2014-09-03 16:54:52
不大,如1-2行 – Mazeryt 2014-09-03 16:57:12
如果你有一個大文件>一些兆字節,你可以結合Xpath + Dom4j和流節點,每個解析的節點應該從dom樹中分離後處理釋放內存。 – 2014-09-03 17:01:59