i am running some java that will tell me how many nodes are in the following xml
There are 7 nodes. i realize that some type of character (i think it might be the retuns) is being counted as well.
my java code is:
q1 - is, how can i see what kind of text node is being represented. My compiled code just gives me a RETURN but i want to be sure...
q2 - how can i/should i turn this off???
i saw a similar post, that is why i am using:
but i dont think it is doing anythign
thanks.....
Code:
<?xml version="1.0"?>
<candy>
<product>Mints</product>
<product>Chocolate</product>
<product>Circus Peanuts</product>
</candy>
There are 7 nodes. i realize that some type of character (i think it might be the retuns) is being counted as well.
my java code is:
Code:
/*
* ShowFile.java
*
* Created on June 22, 2007, 11:17 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
public class ShowFile
{
public static void main(String args[])
{
File docFile = new File("/Volumes/Data/projects/java/Tutorialized/src/candy.xml");
Document doc = null;
// Create a new document object called doc that represents the data stored in our xml
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(docFile);
}
catch(java.io.IOException e)
{
System.out.println("Cant find the file");
}
catch(Exception e)
{
System.out.println("Problem parsing " + docFile);
}
// If you have gotten this far with out an exception the file is parsed
System.out.println(docFile + " parsed successfully");
// Reference the root element of the document
Element root = doc.getDocumentElement();
System.out.println("the root element is " + root.getNodeName());
/* Now that we have a name for our root element, we can get his children.
* to do this we use getChildNodes and nodeList. We will then use a
* println statement to show us how many child nodes there are. */
NodeList kidList = root.getChildNodes();
System.out.println("There are " + kidList.getLength() + "child nodes that belong to " + root.getNodeName());
System.out.println("The node type and coinciding info:");
/* Now we are going iterate the total amound of nodes and disply there
* type (TEXT or ELEMENT) if they are TEXT we show value, EXLEMENT we
* whow name... */
// This is a funky for looop
for(Node child = root.getFirstChild(); child != null; child=child.getNextSibling())
{
if(child.getNodeType() == child.TEXT_NODE)
{
System.out.println("TEXT NODE, with a value of " + child.getNodeValue() + "...");
}
else if(child.getNodeType() == child.ELEMENT_NODE)
{
System.out.println("ELEMENT NODE with a node name of " + child.getNodeName() +
" and a value of " + child.getFirstChild().getNodeValue()+ "...");
}
}
}
}
q1 - is, how can i see what kind of text node is being represented. My compiled code just gives me a RETURN but i want to be sure...
q2 - how can i/should i turn this off???
i saw a similar post, that is why i am using:
Code:
dbf.setIgnoringElementContentWhitespace(true);
thanks.....