【问题标题】:How to convert string xml to Map<String,String>如何将字符串 xml 转换为 Map<String,String>
【发布时间】:2012-05-15 09:54:58
【问题描述】:

如何将 xml 的元素和属性的所有值转换为字符串映射? 是否有一些图书馆可以做到这一点?我找到了库 xStream 但我不知道如何配置它。

【问题讨论】:

标签: java xml map


【解决方案1】:

我只是想要这个:

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;
}

【讨论】:

  • 也许它以某种方式起作用,但不要忘记更正: Node item = currentNode.getAttributes().item(j); (j 而不是 i)。此外,它不会收集相同的标签,如 stackoverflow.com/a/31245219/2914140 所示(该解决方案也有错误)。
【解决方案2】:

Underscore-java库可以将HashMap转换为xml,反之亦然。

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"));
    }
}

【讨论】:

    【解决方案3】:

    试试下面的代码:

    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();
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-20
      • 2018-02-14
      • 2011-02-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      • 2013-05-24
      • 2014-01-29
      相关资源
      最近更新 更多