将XML String转换为Map并获取密钥&使用Java的值对 [英] Convert XML String to Map and get the key & value pairs using Java

查看:133
本文介绍了将XML String转换为Map并获取密钥&使用Java的值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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作为关键字和相应的值。有更好的想法吗?

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

推荐答案

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。

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

这篇关于将XML String转换为Map并获取密钥&amp;使用Java的值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆