Try this:
/**
* This method is adapted from: Using the IBM XML parser (XML4J)
* written by: LindaMay Patterson
* Creation date: (01/30/01 2:01:37 PM)
*/
public static void printElement(Element element) throws IOException
{
int x;
if (element.hasChildNodes())
{ //***** If this element has a value or sub-elements
//** For each child, if the child is an element call print element
to print that
//** portion of the tree, if it is a text node, print the text to
stdout.
//** All other node types are ignored for the sake of simplicity.
NodeList children = element.getChildNodes();
for (x = 0; x < children.getLength(); x++)
{
int type = children.item(x).getNodeType();
switch (type)
{
// print element nodes
case children.item(x).ELEMENT_NODE:
{
printElement((Element)children.item(x));
break;
}
// handle CDATA Section
case children.item(x).CDATA_SECTION_NODE:
{
System.out.print(children.item(x).getNodeValue());
break;
}
// Print Text Nodes
case children.item(x).TEXT_NODE:
{
System.out.print(children.item(x).getNodeValue());
break;
}
}
}// end for loop
}// end outer if
}// end method
You can invoke it by
printElement(doc.getDocumentElement());
Where doc is a DOM document.
There are several types of nodes. Element, CDATA,and text to name a few.
If you try to print out an element node value you get null. I suspect that
is the problem you were having?
Allison M. Santoro
Enterprise Application Development
IBM Global Services
580 Walnut Cincinnati, Ohio
513-762-2367 T/L 663-2367
"Cory Hubert" <[EMAIL PROTECTED]> on 02/20/2001 12:34:50 PM
Please respond to [EMAIL PROTECTED]
To: <[EMAIL PROTECTED]>
cc:
Subject: Problem with DOMParser
Hello. I am having some issues using the DOMParser. I read the
Documentation. And I believe I am doing everything correctly. The
following
code reads in the xmlResponse.xml document correctly, you can even get the
name of the Nodes within the document. BUT you can't retrieve thier
values.
Any Idea what could be the issue?
DOMParser p = new DOMParser();
InputSource source = new InputSource("xmlResponse.xml");
p.parse(source);
Document doc = p.getDocument();
Element docElement = doc.getDocumentElement();
for (int i =0; i < docElement.getChildNodes().getLength();
i++)
{
System.out.println(docElement.getChildNodes
().item(i).getNodeName());
System.out.println(docElement.getChildNodes
().item(i).getNodeValue());
}
Invertica
:Cory L Hubert
:Senior Software Engineer
:[p]646-792-6588
:[f]212-571-3588
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]