java - 使用 Java 将 XML String 转换为 Map 并获取键值对

标签 java xml dictionary xstream

我有一个 XML 字符串。我正在尝试将该字符串转换为映射,以便我可以获得键和值。但是它无法转换。这是我的代码

String xmlString = "<?xml version="1.0" encoding="UTF-8"?><user>
                        <kyc></kyc>
                        <address></address>
                        <resiFI></resiFI></user>"

def convertStringToDocument = {
        xmlString ->
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder;
            try {
                builder = factory.newDocumentBuilder();
                org.w3c.dom.Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
                return doc;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
    }
    def populateDocProofsFromWaiversXML = {
        xmlString, mandateFlag ->

            final List<DocumentProof> documentProofs = new ArrayList<DocumentProof>();
            if (xmlString != null) {
                try {
                    HashMap<String, String> values = new HashMap<String, String>();
                    Document xml = convertStringToDocument(waiversList);
                    org.w3c.dom.Node user = xml.getFirstChild();
                    NodeList childs = user.getChildNodes();
                    org.w3c.dom.Node child;
                    for (int i = 0; i < childs.getLength(); i++) {
                        child = childs.item(i);
                        System.out.println(child.getNodeName());
                        System.out.println(child.getNodeValue());
                        values.put(child.getNodeName(), child.getNodeValue());
                    }
                }  catch (Throwable t) {
                    println "error"
                    //LOG.error("Could not set document proofs from waivers ", t);
                }
            }
            return documentProofs;
    }

我想获取“kyc”作为键和相应的值。还有更好的想法吗?

最佳答案

package com.test;

import java.io.StringReader;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class Random {

    /**
     * @param args
     */
    public static void main(String[] args) {
        HashMap<String, String> values = new HashMap<String, String>();
        String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><user><kyc>123</kyc><address>test</address><resiFI>asds</resiFI></user>";
        Document xml = convertStringToDocument(xmlString);
        Node user = xml.getFirstChild();
        NodeList childs = user.getChildNodes();
        Node child;
        for (int i = 0; i < childs.getLength(); i++) {
            child = childs.item(i);
            System.out.println(child.getNodeName());
            System.out.println(child.getTextContent());
            values.put(child.getNodeName(), child.getTextContent());
        }

    }

    private static Document convertStringToDocument(String xmlStr) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        try {
            builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new InputSource(new StringReader(
                    xmlStr)));
            return doc;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

这会起作用。请检查 :) 您可以使用 DOM。

关于java - 使用 Java 将 XML String 转换为 Map 并获取键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32735474/

相关文章:

python - 计算候选人被投票的次数

dictionary - 免费字符串字典(名词)

java - jtable - 访问行后面的对象

java - Play Framework [2.3.0 - Java] 在测试期间访问 play.Configuration.root()

ios - XML 解析器仅加载第一个对象

python - python 如何为 GraphViz 编写一个点文件,要求将某些边染成红色?

java - 在 String.matches() 方法中转义句点时遇到问题

java - 是否可以通过编程方式确定文件移动是否会产生副本?

xml - 使用 lift 将 xml 转换为 Json 的行为很奇怪

xml - 如何使用 XQuery 组合多个 XML 文件