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?