Reputation: 2500
Here is my xml
<body>
<map>
<key1>
value1
</key1>
<key2>
value2
</key2>
....
</map>
<body>
And i have:
Document xPacket;
XPath xPath = XPathFactory.newInstance().newXPath();
Map<Integer, String> temp = new HashMap<Integer, String>();
Object rawMap = xPath.compile("//body/map/").evaluate(xPacket, XPathConstants.NODESET);
NodeList mapNodeList = (NodeList) rawMap;
but how to iterate through NodeList
and fill values in map?
Upvotes: 0
Views: 47
Reputation: 5472
Since you don't specify otherwise, I'm assuming you are using classes of the standard Java library.
The NodeList
object has an item(int)
method. You can use that method to loop through the nodes in the NodeList
:
Map<Integer, String> map = new HashMap();
for (int i = 0; i < aNodeList.getLength(); i++) {
Node item = aNodeList.item(i);
map.put(Integer.valueOf(item.getTextContent()), item.getLocalName());
}
Here I assumed that you wish to map the text content of the XML key nodes to the nodes' names (because of the type parameters of your map).
Upvotes: 1