How I can convert all value of element and attributes of xml to Map of string ? Is there some library which can do this ? I found library xStream but I dont know how to configure it.
Asked
Active
Viewed 2.3k times
2
-
1http://stackoverflow.com/questions/373833/best-xml-parser-for-java – assylias May 15 '12 at 09:57
-
1possible dublicate http://stackoverflow.com/questions/1537207/how-to-convert-xml-to-java-util-map-and-vice-versa – isvforall May 15 '12 at 09:58
-
1Also, you might want to view http://stackoverflow.com/questions/8296126/extract-xml-information-to-listmap-using-xpath as it provides a good example of how to do this. – Ewald May 15 '12 at 09:58
-
nope sorry none of this answers can help me – hudi May 15 '12 at 11:11
-
@hudi: That's because your question is far too vague. – skaffman May 15 '12 at 11:23
3 Answers
7
I just wanna this:
public static Map<String, String> convertNodesFromXml(String xml) throws Exception {
InputStream is = new ByteArrayInputStream(xml.getBytes());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(is);
return createMap(document.getDocumentElement());
}
public static Map<String, String> createMap(Node node) {
Map<String, String> map = new HashMap<String, String>();
NodeList nodeList = node.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node currentNode = nodeList.item(i);
if (currentNode.hasAttributes()) {
for (int j = 0; j < currentNode.getAttributes().getLength(); j++) {
Node item = currentNode.getAttributes().item(i);
map.put(item.getNodeName(), item.getTextContent());
}
}
if (node.getFirstChild() != null && node.getFirstChild().getNodeType() == Node.ELEMENT_NODE) {
map.putAll(createMap(currentNode));
} else if (node.getFirstChild().getNodeType() == Node.TEXT_NODE) {
map.put(node.getLocalName(), node.getTextContent());
}
}
return map;
}

hudi
- 15,555
- 47
- 142
- 246
-
Maybe it works in some way, but don't forget to correct: Node item = currentNode.getAttributes().item(j); (j instead of i). Also it doesn't collect equal tags, as can be seen at http://stackoverflow.com/a/31245219/2914140 (that solution also has bugs). – CoolMind Sep 19 '15 at 10:30
2
Underscore-java library can convert HashMap to xml and vice verse.
import com.github.underscore.U;
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("name", "chris");
map.put("island", "faranga");
System.out.println(U.toXml(map));
Map<String, Object> newMap = U.fromXmlMap(U.toXml(map));
System.out.println(newMap.get("name"));
}
}

Valentyn Kolesnikov
- 2,029
- 1
- 24
- 31
0
Try the code below:
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("C://logs//XML.xml");
NodeList nodeList = doc.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node textChild = nodeList.item(i);
NodeList childNodes = textChild.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node grantChild = childNodes.item(j);
NodeList grantChildNodes = grantChild.getChildNodes();
for (int k = 0; k < grantChildNodes.getLength(); k++) {
if(!StrUtl.isEmptyTrimmed( grantChildNodes.item(k).getTextContent() ) ) {
Map<String, String> map = new HashMap<String, String>();
map.put(grantChildNodes.item(k).getNodeName() , grantChildNodes.item(k).getTextContent());
System.out.println(map);
}
}
}
}
}catch (Exception e){
e.printStackTrace();
}