0

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

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

4
  • possible duplicate here: stackoverflow.com/questions/1537207/… Commented Sep 23, 2015 at 9:29
  • try to use dom xml parser Commented Sep 23, 2015 at 9:32
  • @virendrao, Can you plz provide a snippet of example? Commented Sep 23, 2015 at 9:32
  • @Nizam check below answer Commented Sep 23, 2015 at 9:40

1 Answer 1

7
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.

Sign up to request clarification or add additional context in comments.

10 Comments

It says "Could not find matching constructor for: java.io.StringReader(com.pramati.org.apache.xerces.dom.DeferredElementImpl)" at this line "Document doc = builder.parse(new InputSource(new StringReader("
@Nizam are imports of class same for StringReader ? which version java you have ? i have this code running on 1.5
I'm using 1.7 java version
are imports proper in code i posted and what you are running? can you just copy paste above code in ide and try to run
Please send your email.. I'll send you the code there.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.