This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes XML and Related Technologies and the fly likes Retrieve CDTA content from XML file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Retrieve CDTA content from XML file" Watch "Retrieve CDTA content from XML file" New topic
Author

Retrieve CDTA content from XML file

Mirco H Schmidt
Greenhorn

Joined: Aug 25, 2010
Posts: 7

Hi,
I have a problem getting the CDATA section from my XML file:
<?xml version="1.0" encoding="UTF-8"?>
<ResultSet rows="37">
<Row index="0">
<Yanumber name="YANumber" type="varchar"><![CDATA[Y00042]]></Yanumber>
<Ybnumber name="YBNumber" type="varchar"><![CDATA[YB01329]]></Ybnumber>
</Row>
</ResultSet>


How can I retrieve in a loop all values from subelements Row in this case Yanumber and Ybnumber?
I tried using XPATH, JDOM
Document doc = new SAXBuilder().build("myxml.xml");
List<?> list = XPath.selectNodes(doc, "ResultSet/Row/Yanumber/text()");
for (Object object : list) {
if (object instanceof CDATA) {
CDATA output = (CDATA) object;
System.out.println(output.getValue());
}
}

... but this gave me only the Yanumber part.
How can I get all parts in a list ?

Thanks for your help
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
That appears to be doing exactly what your xpath expression asked for:


Yanumber is what you specified.

If this was my problem I would get the Row Element, get a NodeList for all the child nodes, then iterate through the child nodes looking for Element nodes.

Note that all the children of Row will include Text type nodes as well as the Yanumber and Ybnumber Elements.

Bill
Mirco H Schmidt
Greenhorn

Joined: Aug 25, 2010
Posts: 7

thanks...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retrieve CDTA content from XML file