将 XML 字符串转换为 Map 并使用 Java 获取键值对

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32735474/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:41:28  来源:igfitidea点击:

Convert XML String to Map and get the key & value pairs using Java

javaxmldictionaryxstream

提问by Syed

I have an XML String. I'm trying to convert that string into map so that I can get key & value. However its not able to convert. Here is my code

我有一个 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;
    }

I'd like to get "kyc" as key and the respective value. Any better ideas?

我想将“kyc”作为键和相应的值。有什么更好的想法吗?

回答by virendrao

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

This will work. Please check :) You can play with DOM.

这将起作用。请检查:) 你可以玩 DOM。