Daku Daku
Daku Daku

Reputation: 115

Convert Node to String

I have a xml parser which was written by someone I hired, a few months ago. The xml is pulled in from a third party. This party, recently put a new important tag on the xml, called SourceName. I messed with the code a bit, and I got most of it I believe, but I get a weird output.

<SourceInfo>
   <SourceName1>NYT</SourceName1>
   <SourceName2>BDN</SourceName2>
</SourceInfo>

NodeList sourceList = element.getElementsByTagName("SourceName1");
    if(sourceList.getLength()>0){
        Element sourceElem=(Element) sourceList.item(0);
        Node sourceName=(Node) sourceElem.getChildNodes().item(0);
        System.out.println(sourceName);
    }

the unwanted return is [#text: NYT] rather than a string of NYT.

So, the question is, how do I convert Node sourceName to String sourceName of the value NYT.

Thanks in advance.

Upvotes: 0

Views: 8774

Answers (1)

emory
emory

Reputation: 10891

I think this is what you are looking for:

System.out.println(sourceName.getNodeValue()); 

* Jochen's comment is valid. I assumed that you were using the DOM parser built in to java. If not, then this answer is probably wrong.

Upvotes: 1

Related Questions