File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Head First Android this week in the Android 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

Retrieve CDTA content from XML file

Mirco H Schmidt

Joined: Aug 25, 2010
Posts: 7

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>

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;

... 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

Joined: Mar 22, 2000
Posts: 12982
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.

Mirco H Schmidt

Joined: Aug 25, 2010
Posts: 7

It is sorta covered in the JavaRanch Style Guide.
subject: Retrieve CDTA content from XML file
jQuery in Action, 3rd edition