Why isn't my DOM search code working?

P

Paul Lee

Hi all,
I'm trying to use a Java written search code to traverse a DOM
hierarchy.
I'm mainy using code that I obtained from the Sun website. Basically,
in
main(), I have

Element rootElement = XML_document.getDocumentElement();

found_element = findElementNode("ATO_RawXML",
XML_document.getDocumentElement() );

where XML_document is of type Document.


The findElementNode function looks like this:

public static Node findElementNode(String name, Node node)
{

Node matchingNode = null;

//Check to see if root is the desired element. If so return root.
String nodeName = node.getNodeName();

if((nodeName != null) & (nodeName.equals(name)))
return node;

//Check to see if root has any children if not return null
if(!(node.hasChildNodes()))
return null;

//Root has children, so continue searching for them
NodeList childNodes = node.getChildNodes();
int noChildren = childNodes.getLength();
for(int i = 0; i < noChildren; i++){
if(matchingNode == null){
Node child = childNodes.item(i);
matchingNode = findElementNode(name,child);
} else break;

}

return matchingNode;

}

and my XML_document looks like:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<WfMessage>
<WfMessageHeader>
<ResponseRequired>Yes</ResponseRequired>
<UserContext>This data is sent back in response</UserContext>
</WfMessageHeader>
<ProcessTemplateExecute>
<ProcTemplateName>Generate And Disseminate FLY_</ProcTemplateName>
<ProcInstName>FLYPRO_Gen#1</ProcInstName>
<KeepName>true</KeepName>
<ProcInstInputData>
<_ACTIVITY_INFO>
<Priority>1</Priority>
</_ACTIVITY_INFO>
<ATO_ATO>
<ATO_FileLoadedIntoSystem>No</ATO_FileLoadedIntoSystem>
<ATO_RawXML>XML DATA HERE</ATO_RawXML>
</ATO_ATO>
</ProcInstInputData>
</ProcessTemplateExecute>
</WfMessage>

Using IBM WSAD, I put break points in during the for...next loop in
findElementNode, but it skips over the Node with the <ATO_RawXML>
tags.
In main(), found_element is null. What is wrong?

TIA,

Paul
 
M

Martin Honnen

Paul Lee wrote:

I'm trying to use a Java written search code to traverse a DOM
hierarchy.
I'm mainy using code that I obtained from the Sun website. Basically,
in
main(), I have

Element rootElement = XML_document.getDocumentElement();

found_element = findElementNode("ATO_RawXML",
XML_document.getDocumentElement() );

Is there any reason you can't use the
getElementsByTagName
method of the document node or any element node to solve that?
 
P

Paul Lee

Martin Honnen said:
Paul Lee wrote:



Is there any reason you can't use the
getElementsByTagName
method of the document node or any element node to solve that?


The only reason I didn't use that is because I simply couldn't get it to
work
- maybe I got the syntax wrong (I was using the Sun website as a
reference).

Best wishes

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top