2012-05-15 82 views
1

如何将元素和属性的所有值转换为xml地图? 有没有一些图书馆可以做到这一点?我发现库xStream,但我不知道如何配置它。如何将字符串xml转换为地图<String,String>

+1

http://stackoverflow.com/questions/373833/best-xml-parser-for-java – assylias

+1

可能dublicate http://stackoverflow.com/questions/1537207/how -to-convert-xml-to-java-util-map-and-vice-versa – isvforall

+1

此外,你可能想查看http://stackoverflow.com/questions/8296126/extract-xml-information-to-listmap-使用-xpath,因为它提供了一个很好的示例。 – Ewald

回答

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")); 
    } 
} 
相关问题