Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retrieve CDTA content from XML file

 
Mirco H Schmidt
Greenhorn
Posts: 7
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic