0

I am trying to extract the xml values in java

Below is the xml and I want to extract userUuid from the xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ResponseSet vers="1.0" svcid="session" reqid="3">
    <Response><![CDATA[
        <SessionResponse vers="1.0" reqid="0">
            <GetSession>
                <Session sid="******" stype="user" cid="uid=****" cdomain="o=nhs" maxtime="0" maxidle="0" maxcaching="0" timeidle="0" timeleft="****" state="valid">
                    <Property name="userUuid" value="555524799109"></Property>
                </Session>
            </GetSession>
        </SessionResponse>]]>
    </Response>
</ResponseSet>

I referred this links none of them worked from me

Read XML in Java with XML attributes

How can I read Xml attributes using Java?

Also I tried

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware(true);
dbFactory.setValidating(false);
DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
ByteArrayInputStream inputStream = new ByteArrayInputStream(sXMLData.getBytes("UTF-8"));

Document document = docBuilder.parse(inputStream);

NodeList nodeList = document.getElementsByTagName("Property");
System.out.println(nodeList.getLength());

for (int i = 0; i < nodeList.getLength(); i++) {
    Element element = (Element) nodeList.item(i);

    String el = element.getAttribute("name");
    System.out.println(el);
}
Rmahajan
  • 1,311
  • 1
  • 14
  • 23
  • 1
    `document.getElementsByTagName("ResponseSet");` gets the `` elements, which do not seem to contain a `name` attribute. You'll want to ultimately select the `` elements that you care about and call `propertyElement.getAttribute("name")` on them. If you want to get *all* `` elements from any place in the document then you can use `document.getElementsByTagName("Property");` – xtratic Feb 12 '19 at 16:02
  • oh it's typo .I changed it to `` – Rmahajan Feb 12 '19 at 16:07
  • ohh I Solved ... I have to add just if condition which solved the problem `if(el.equalsIgnoreCase("userUuid")) { System.out.println(element.getAttribute("value")); }` – Rmahajan Feb 12 '19 at 16:12

1 Answers1

0

Just posting an answer if someone faces same issue

public String parseXMLResponse(String sXMLData) throws IOException, SAXException, ParserConfigurationException {

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        dbFactory.setNamespaceAware(true);
        dbFactory.setValidating(false);
        DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
        ByteArrayInputStream inputStream = new ByteArrayInputStream(sXMLData.getBytes("UTF-8"));

        Document document = docBuilder.parse(inputStream);

        NodeList nodeList = document.getElementsByTagName("Property");
        String userUuId = null; 
        for (int i = 0; i < nodeList.getLength(); i++) {
            Element element = (Element) nodeList.item(i);
            String el = element.getAttribute("name");
            if(el.equalsIgnoreCase("UserId"))
            {
                userUuId = element.getAttribute("value");
                break;
            }
        }

        return userUuId;
    }
Rmahajan
  • 1,311
  • 1
  • 14
  • 23