XML DOM processing
I am trying to create a DOM tree for this XML document and then print the value of the TEXT_NODE;
<?xml version="1.0" encoding="utf-8"?>
<TRIGGER>
<EXPRESSION>
<THRESHOLD level="8000">
<STRING name="all.defaultsc">BUSH</STRING>
</THRESHOLD>
</EXPRESSION>
</TRIGGER>
I am trying to find the value of the TEXT_NODE;
When I use the <STRING> tag and the value of Node.getNodeValue() is null; If I replace the <STRING> tag with <STRINGT> then the value of Node.getNodeValue() = BUSH;
Following is the snippet of code I am using
===============================================
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(xml);
======================================================
Thank You,
[1186 byte] By [
vpmorjea] at [2007-10-2 4:30:42]

Part of what you describe makes very good sense.
1) Look at the table at the top of the JavaDoc description for the interface Node. You will see that nodes that are of type Element have a null value for getNodeValue(). So, what you report makes sense.
2) To the the value "BUSH", the normal way is to find the element for STRING, and then get its children. In the way you have shown the XML there will only be one child of STRING and it will be of type Text. If you do the getNodeValue() on a Text node, you will get the string (in this case "BUSH").
3) What I don't understand is the description of how things work when you changed the tag name from STRING to STRINGT. I cannot explain that.
Dave Patterson
import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
public class XMLtoDOM {
private void countLeaves(Node node, int [] nLeaves)
{
Element elt = (Element) node;
Node child = elt.getFirstChild(); // Get child
// identify # of children whose "weight" attribute needs to be set
while (child != null)
{
System.out.println(child.getNodeType() + " : " + child.getNodeValue());
if (child.getNodeType() == Node.ELEMENT_NODE &&
!child.getNodeName().trim().equalsIgnoreCase("NOT"))
{
Element childElt = (Element) child;
if (childElt.getNodeName().trim().equalsIgnoreCase("STRING") ||
childElt.getNodeName().trim().equalsIgnoreCase("WCSTRING") ||
childElt.getNodeName().trim().equalsIgnoreCase("INTEGER") ||
childElt.getNodeName().trim().equalsIgnoreCase("FLOAT") ||
childElt.getNodeName().trim().equalsIgnoreCase("VALUE"))
{
nLeaves[0] ++;
}
else
{
countLeaves(child, nLeaves);
}
}
child = child.getNextSibling(); // Get next child
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String string = "<?xml version=\"1.0\" encoding=\"utf-8\"?> " +
"<TRIGGER> <EXPRESSION> <THRESHOLD level=\"8000\"> " +
"<STRING name=\"all.defaultsc\">BUSH</STRING> " +
"</THRESHOLD> </EXPRESSION> </TRIGGER>";
StringReader reader = new StringReader(string);
org.xml.sax.InputSource is = new org.xml.sax.InputSource(reader);
Document document;
XMLtoDOM xmldoc = new XMLtoDOM();
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(is);
int [] nLeaves = new int[1];
nLeaves[0] = 0;
Node node = document.getFirstChild();
xmldoc.countLeaves(node, nLeaves);
} catch (Exception e) { System.err.println (e); }
}
}