-1

I'd like to read all CDATA associated to a child node and store them into a java ArrayList! If you could suggest a simple and quick way I'd grately appreciate it!

Thanks!

In would like to have a little routine testing the txnid for an specific value and read all the associated CDATA queries in different Array Elements.

 <?xml version="1.0" encoding="utf-8" standalone="yes" ?> 
 <ZQueries txid="Group 1" version="1.0.0.0">
 <ZQuery name="Some Name 1" savename="SomeFileName1.xml">
  <![CDATA[ 
      SELECT ...........

  ]]> 
 </ZQuery>
 <ZQuery name="Some Name 2"   savename="SomeFileName.xml">
 <![CDATA[ 
    SELECT .............


  ]]> 
</ZQuery>
<ZQuery name="some name 3" savename="someFileName.xml">
<![CDATA[ 
    SELECT ..............

 ]]> 
 </ZQuery>
 </ZQueries>
 <ZQueries txid="Group 2" version="1.0.0.0">
 <ZQuery name="Some Name 1" savename="SomeFileName1.xml">
  <![CDATA[ 
      SELECT ...........

  ]]> 
 </ZQuery>
 <ZQuery name="Some Name 2"   savename="SomeFileName.xml">
 <![CDATA[ 
    SELECT .............


  ]]> 

 </ZQueries>

IS THIS THE RIGHT APPROACH! THE FOLLOWING CODE CREATES THE EXCEPTION SHOWN! HOW DO YOU THINK I CAN GET AROUND THIS!

 SAXBuilder builder = new SAXBuilder();
 Document doc = builder.build(in);  //build a JDOM doc from an input stream
 ArrayList<String> queries = new ArrayList<String>();
 Element root = doc.getRootElement();

 Iterator elemIter = root.getDescendants();

 while (elemIter.hasNext()) {
     Element tempElem = (Element) elemIter.next();
     if (root.getChild("ZQueries").getAttributeValue("txnid").equals(tempTxnid)) {
         String CDATA = tempElem.getChildText("ZQueries");
         queries.add(CDATA);
     } 
 }



Exception in thread "AWT-EventQueue-0" java.security.PrivilegedActionException:
java.security.PrivilegedActionException: org.jdom.input.JDOMParseException: Error on 
line 1: Premature end of file.

2 Answers 2

0

Use the SAX Parser :

try {
    // Create a builder factory
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    // Configure it to coalesce CDATA nodes
    factory.setCoalescing(true);

    // Create the builder and parse the file
    // Perform your Parsing here 

    // doc will not contain any CDATA nodes
} catch (SAXException e) {
    // A parsing error occurred; the xml input is not valid
} catch (ParserConfigurationException e) {
} catch (IOException e) {
}
2
  • <?xml version="1.0" encoding="utf-8" standalone="yes" ?> - <ZQueries txnid="Default Group" version="1.0.0.0"> - <ZQuery name="some name" savename="somefile.xml"> - <![CDATA[ SELECT .................... ]]> </ZQuery>
    – Sam
    Oct 14, 2011 at 17:44
  • Sorry I have made this query available in the actual question area and added a little more description to it to show what I exactly mean. Now with your method, how am I supposed to store the CDATA which are some queries into an Array??!
    – Sam
    Oct 14, 2011 at 17:51
0

Look into using JDOM. Before you parse the document, create an Array list, probably of type string. Iterate over the elements and when you hit a ZQuery element, use element.getChildText("ZQuery") which will return all the CDATA info without the CDATA tags.

FileInputStream fis = new FileInputStream(file); //where file holds the data to parse. 

Can also use StringBuilder
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(fis); //build a JDOM doc from file
ArrayList<String> arl = new ArrayList<String>();
Element root = doc.getRootElement();
Iterator elemIter = root.getDescendants(); //you can put filters on this to only get specific types of elements ie: root.getDescendants("ZQuery"); will give you an iterator over just ZQuery elements. 

while (elemIter.hasNext()) {
    Element foo = (Element) elemIter.next();
    String CDATA = foo.getChildText("ZQuery")
    arl.add(CDATA);
}

Additional Resources:

6
  • If you could give me a little demo of how this is done, I would be very much grateful! My knowledge of XML is very limited!
    – Sam
    Oct 14, 2011 at 18:30
  • @Sam done. If you want to learn more about XML, I suggest going to the w3schools.com website and looking up their tutorials on XML. It'll give you some insight into how it works.
    – FloppyDisk
    Oct 14, 2011 at 18:48
  • Thanks a million! Now how would you alter this code so that when a "txid" of certain value is reached, the contents of its associated CDATA be read and stored in the array??! Again thank you heaps for your kind help!
    – Sam
    Oct 14, 2011 at 19:04
  • You're welcome. You'll learn a lot more if you try to write it yourself than if I do it for you. I added some links to additional resources to learn about JDOM parsing. You parse more elements, get attributes, etc. Reading the JDOM javadoc and those tutorials will put you on the path towards where you want to go. All you would need to add is a flag on the txid attribute when you parse the element holding it. Mess around with setting up the JDOM parser! You'll learn a lot!
    – FloppyDisk
    Oct 14, 2011 at 19:32
  • 1
    There is no 'recipe' that anyone can give you for this. it's not a single call to just the right member function. The task at hand is a basic application of bread-and-butter XML processing. You need to take the time to learn how to use StaX or DOM or JDOM or whatever appeals, and then this will be easy.
    – bmargulies
    Oct 14, 2011 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.