Looking for a better way to extract the CDATA elments
I'm trying to use the following to create a document :
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(false);
// Create the builder and parse the file
Document doc = factory.newDocumentBuilder().parse(f);
is there something else I need to parse the document correctly? The reason I ask is because, once the element is isolated that contains the necessary information, I'm having to use the following to extract the contents from the CDATA section:
value.addElement(objElmProp.getFirstChild().getNextSibling().getFirstChild().getNodeValue());
The element structure looks like this (Note: I have no control or input over how this element is created):
<property name="propertyValue">
<string><![CDATA[$USER_HOME$$/$MYDIR]]></string>
</property>
objElmProp.getNodeName() finds 'property' which is the above CDATA_SECTION_NODE, but contains useless information for the value ('\n\t\t\t\t\t' is what this element contains for value). In addition, any children are also null or garbage characters.
Another reason I believe there is an issue is because I cannot get changes made to the instance to show up in a new document using the following approach:
// Write the DOM document to the file
Transformer xformer =TransformerFactory.newInstance().newTransformer();
Result result = new StreamResult(new FileOutputStream(file));
xformer.transform(source, result);
I'd appreciate any assistance you can provide.
Thanks,
Sean
[1659 byte] By [
smurphya] at [2007-10-3 3:30:23]

This is the most common problem with DOM.
The property element has three child nodes.
1) A Text node with "\n" as its text value.
2) An Element with a name of string
3) A Text node with "\n" as its text value.
If you always assume this sequence, you can use code similar to what you have done. But, if at some day in the future, the creator of this code decides to create
<property name="propertyValue"><sttring> whatever</string></property>
your code will not work.
The only two safe ways to do it are to use XPath to find the string element that is a child of a property element, or to loop through the child nodes of the property element and ignore any that are Text nodes.
Go read the javadoc for the Node interface. There is a table at the top that shows what kinds of nodes have what attributes.
Dave Patterson
Thank you very much for the response. This was very helpful in getting to the Node in question. However, I'm still seeing a problem with getting the values written to the translated document. When I do a setNodeValue to "This is it" (from the prior value of "Whatever" as below).
<string><![CDATA[Whatever]]></string></property>
getlenth of innerStringNodes : 1
inCDATANode name : #cdata-section
inCDATANode value : This is it
I can see the debugger picks up the new value. However, the new result doesn't persist once I run the transform:
Result result = new StreamResult(new FileOutputStream(file));
xformer.transform(source, result);
'source' should be holding all the changes, but the CDATA field changes are lost.
Any ideas on what I'm doing wrong?