java 如何将字符串 xml 转换为 Map<String,String>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10598250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert string xml to Map<String,String>
提问by hudi
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.
如何将 xml 的元素和属性的所有值转换为字符串映射?是否有一些图书馆可以做到这一点?我找到了库 xStream 但我不知道如何配置它。
回答by hudi
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;
}
回答by Valentyn Kolesnikov
Underscore-javalibrary can convert HashMap to xml and vice verse. I am the maintainer of the project.
Underscore-java库可以将 HashMap 转换为 xml,反之亦然。我是项目的维护者。
import com.github.underscore.lodash.U;
import java.util.*;
public class Main {
@SuppressWarnings("unchecked")
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 = (Map<String, Object>) U.fromXml(U.toXml(map));
System.out.println(newMap.get("name"));
}
}
回答by raghav
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();
}