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]
# 1

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

d.pattersona at 2007-7-16 0:00:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
Could it be that STRING is a primitive type in XML and therefore the Node.getNodeValue() for TEXT_NODE of the xml <STRING> BUSH </STRING>, is null? What would be a work around? Thank You for your help;
vpmorjea at 2007-7-16 0:00:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
That would depend entirely on how you are trying to get the value of the text node. Would you like to post the code that does that?
DrClapa at 2007-7-16 0:00:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
> STRING is a primitive type in XMLNo, this is false. No element name in XML has any meaning at all.
DrClapa at 2007-7-16 0:00:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

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); }

}

}

vpmorjea at 2007-7-16 0:00:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6
Whoever wrote that class put code in it that specifically does not look at the children when the element name is STRING. It does look at the children when the element name is STRINGT, though, because that isn't in the list of "special" element names that are treated as "leaves".
DrClapa at 2007-7-16 0:00:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7
Thank you very much for solving the problem.
vpmorjea at 2007-7-16 0:00:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...