代码之家  ›  专栏  ›  技术社区  ›  Syed

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

  •  0
  • Syed  · 技术社区  · 9 年前

    我有一个XML字符串。我正在尝试将该字符串转换为map,以便获得key&价值然而,它无法转换。这是我的代码

    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”作为键和相应的值。有更好的主意吗?

    1 回复  |  直到 9 年前
        1
  •  7
  •   virendrao    9 年前
    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。