如何将元素和属性的所有值转换为xml地图? 有没有一些图书馆可以做到这一点?我发现库xStream,但我不知道如何配置它。如何将字符串xml转换为地图<String,String>
1
A
回答
5
我只是想这一点:
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;
}
+0
也许它以某种方式工作,但不要忘记更正:Node item = currentNode.getAttributes()。item(j); (j而不是i)。它也不收集相同的标签,可以在http://stackoverflow.com/a/31245219/2914140看到(该解决方案也有错误)。 – CoolMind
0
尝试下面的代码:
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();
}
0
Underscore-lodash库可以HashMap中转换为XML,反之亦然。
代码例如:
import com.github.underscore.lodash.$;
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($.toXml(map));
Map<String, Object> newMap = (Map<String, Object>) $.fromXml($.toXml(map));
System.out.println(newMap.get("name"));
}
}
相关问题
- 1. 如何将xml字符串转换为映射<String,String>
- 2. 转换地图<String,字符串>地图<字符串,对象>
- 3. 如何使用Google集合将地图<字符串,字符串>转换为列表<String>
- 4. 如何将Json字符串转换为List <HashMap <String,String >>?
- 5. 如何将字符串转换为SortedMap <String,SortedMap <String,Object >>?
- 6. 我如何将IQueryable <string>转换为字符串数组?
- 7. 如何将IQueryable <string>转换为字符串?
- 8. 如何将字符串转换为ArrayList的<String>
- 9. Apache Hive:将Map <string,string>转换为json字符串?
- 10. 的Java 8转换列表<地图<String,字符串>地图<字符串,地图<String,字符串>>
- 11. 如何将字符串转换为字符串数组String()?
- 12. 如何将字符串转换为xml
- 13. 类型转换地图<String,字符串>地图<对象,对象>
- 14. 的Java转换地图<字符串,对象>到地图<String, ?>
- 15. 如何将Dictionary <string,string>转换为LINQ中的属性字符串?
- 16. 如何使用System.Json将Dictionary <string,string>转换为json字符串?
- 17. 转换字符串[] []为String []
- 18. 转换地图<字符串,字符串>为JSON
- 19. 元帅地图<String,字符串>为.xml
- 20. java-如何将一个ArrayList <ArrayList <String>>转换为字符串[]] []
- 21. 将地图<字符串,对象>转换为地图<字符串,列表<Object>>
- 22. 如何将转换为字符串值的[[String]]转换回原始类型[[String]]?
- 23. 将数组<string>转换为字符串pyspark数据框
- 24. 将字符串转换为列表<string>在一行中?
- 25. 使用java将字符串[]转换为Arraylist <String>
- 26. 将字符串数组转换为列表<string>
- 27. 将char转换为堆栈的字符串<string>
- 28. 将列表<String>转换为Java中的字符串[]
- 29. 将字符串转换为ArrayList <String> in Java
- 30. 将字符串[]转换为ObservableCollection <string>
http://stackoverflow.com/questions/373833/best-xml-parser-for-java – assylias
可能dublicate http://stackoverflow.com/questions/1537207/how -to-convert-xml-to-java-util-map-and-vice-versa – isvforall
此外,你可能想查看http://stackoverflow.com/questions/8296126/extract-xml-information-to-listmap-使用-xpath,因为它提供了一个很好的示例。 – Ewald