Why isn't my DOM search code working?

Discussion in 'XML' started by Paul Lee, Jan 26, 2004.

  1. Paul Lee

    Paul Lee Guest

    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
    Paul Lee, Jan 26, 2004
    #1
    1. Advertising

  2. 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?
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jan 26, 2004
    #2
    1. Advertising

  3. Paul Lee

    Paul Lee Guest

    "Martin Honnen" <> wrote in message
    news:40154a96$...
    >
    >
    > 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?



    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
    --
    http://www.paullee.com
    Paul Lee, Jan 26, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    325
    Patricia Shanahan
    May 22, 2005
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,836
    Smokey Grindel
    Dec 2, 2006
  3. Simon
    Replies:
    9
    Views:
    313
    Default User
    Jul 18, 2006
  4. Adam Penny
    Replies:
    4
    Views:
    105
    Brian Candler
    Oct 21, 2008
  5. Replies:
    6
    Views:
    96
    Mark Lawrence
    Oct 12, 2013
Loading...

Share This Page