jitendra varshney
jitendra varshney

Reputation: 3562

how to parse and get data from xml in java

I have this XML code:

 <?xml version="1.0" encoding="utf-8"?>
 <string xmlns="https://www.cvlkra.com/">tTKyEndh0iBqnZdjpUntEQ%3d%3d</string>

I want to get this: tTKyEndh0iBqnZdjpUntEQ%3d%3d for which I have tried the below code:

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder1 = factory.newDocumentBuilder();
    Document document = builder1.parse(new InputSource(new StringReader(string)));
    Element rootElement = document.getDocumentElement();
    String nodeName = rootElement.getNodeName();

But i am not getting it. I am getting null value instead of tTKyEndh0iBqnZdjpUntEQ%3d%3d even when I have tried some other code also.

Upvotes: 0

Views: 39

Answers (2)

SamHoque
SamHoque

Reputation: 3154

Try using getTextContent() instead getNodeValue() returns null because it has no values.

Upvotes: 0

MyOggRadio
MyOggRadio

Reputation: 32

You should not use getNodeName() instead use rootElement.getNodeValue(). May be this helps.

Upvotes: -1

Related Questions