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);
}
3
  • 1
    document.getElementsByTagName("ResponseSet"); gets the <ResponseSet> elements, which do not seem to contain a name attribute. You'll want to ultimately select the <Property> elements that you care about and call propertyElement.getAttribute("name") on them. If you want to get all <Property> elements from any place in the document then you can use document.getElementsByTagName("Property"); Commented Feb 12, 2019 at 16:02
  • oh it's typo .I changed it to <Property> Commented Feb 12, 2019 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")); } Commented Feb 12, 2019 at 16:12

1 Answer 1

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

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.