Wrox Home  
Search P2P Archive for: Go

  Return to Index  

apache_xml thread: Is this how XML Parsers were meant to be used?


Message #1 by "Peter Foti (PeterF)" <PeterF@S...> on Mon, 1 Jul 2002 10:26:51 -0400
I'm using Xerces and I have an XML file that has this general structure:

<a>
   <b>
      <c>abc</c>
      <c>def</c>
      <c>ghi</c>
   </b>
</a>

Now, I need to get the entire document, so I am using the DOM parser.  I
used the DOMPrint sample as my guide.  So I had to write my own c++ file
that contained the following sequence:

1. Initialize the XML engine
2. Create a DOMParser object
3. parse my file.
4. Catch any exceptions
5. If no errors, create a DOM_Node to hold the document (using getDocument)
6. Create a DOM_Node (aNode) to iterate through the top level elements (<a>,
and if there was any processing instructions, etc.)
7. Create a DOM_Node (child1) to iterate through the <b> level elements
8. Create a DOM_Node (child2) to iterate through the <c> level elements
9. Create a DOM_Node (child3) to get the text withing <c>
10. while( aNode != 0 ), and then do strcmp on
(aNode.getNodeName()).transcode() to find the "a" element.
11. Once we find the "a" element, child1 = aNode.getFirstChild().
12. while( child1 != 0 ), and then do strcmp on
(child1.getNodeName()).transcode() to find the "b" element.
13. Once we find the "b" element, child2 = child1.getFirstChild()
14. while( child2 != 0), and then do strcmp on
(child2.getNodeName()).transcode() to find the "c" element.
15. Once we find the "c" element, child3 = child2.getFirstChild()
16. while ( child3 != 0 ), and then we get the string value.
17. delete parser and Terminate.

Note that at the end of each while loop is a getNextSibling() function call
for each of the DOM_Node objects.

Is this how the XML parser is meant to be used?  Is there an easier way to
find the element I'm looking for?  It seems that in this example, if my XML
changed to include another level somewhere in there, then I would need to
re-write my code and recompile.  Am I doing this all wrong?

Thanks!  :)

Peter Foti
Systolic Networks
Phone:  (xxx) xxx-xxxx
Fax:  (xxx) xxx-xxxx


Message #2 by "Peter Foti (PeterF)" <PeterF@S...> on Mon, 1 Jul 2002 14:31:00 -0400
Ok, I have done some more digging and it looks like I was close with my
example below.  However, the getElementsByTagName() method should probably
be what I use.  The only problem is that this returns a NodeList, and if I
access one of the items in the list (using index()), then I get a node
instead of an element, so can I use getElementsByTagName repeatedly to dig
down into the DOM?  

Thanks,
Pete


> -----Original Message-----
> From: Peter Foti (PeterF) [mailto:PeterF@S...]
> Sent: Monday, July 01, 2002 10:27 AM
> To: Apache XML
> Subject: [apache_xml] Is this how XML Parsers were meant to be used?
> 
> 
> I'm using Xerces and I have an XML file that has this general 
> structure:
> 
> <a>
>    <b>
>       <c>abc</c>
>       <c>def</c>
>       <c>ghi</c>
>    </b>
> </a>
> 
> Now, I need to get the entire document, so I am using the DOM 
> parser.  I
> used the DOMPrint sample as my guide.  So I had to write my 
> own c++ file
> that contained the following sequence:
> 
> 1. Initialize the XML engine
> 2. Create a DOMParser object
> 3. parse my file.
> 4. Catch any exceptions
> 5. If no errors, create a DOM_Node to hold the document 
> (using getDocument)
> 6. Create a DOM_Node (aNode) to iterate through the top level 
> elements (<a>,
> and if there was any processing instructions, etc.)
> 7. Create a DOM_Node (child1) to iterate through the <b> 
> level elements
> 8. Create a DOM_Node (child2) to iterate through the <c> 
> level elements
> 9. Create a DOM_Node (child3) to get the text withing <c>
> 10. while( aNode != 0 ), and then do strcmp on
> (aNode.getNodeName()).transcode() to find the "a" element.
> 11. Once we find the "a" element, child1 = aNode.getFirstChild().
> 12. while( child1 != 0 ), and then do strcmp on
> (child1.getNodeName()).transcode() to find the "b" element.
> 13. Once we find the "b" element, child2 = child1.getFirstChild()
> 14. while( child2 != 0), and then do strcmp on
> (child2.getNodeName()).transcode() to find the "c" element.
> 15. Once we find the "c" element, child3 = child2.getFirstChild()
> 16. while ( child3 != 0 ), and then we get the string value.
> 17. delete parser and Terminate.
> 
> Note that at the end of each while loop is a getNextSibling() 
> function call
> for each of the DOM_Node objects.
> 
> Is this how the XML parser is meant to be used?  Is there an 
> easier way to
> find the element I'm looking for?  It seems that in this 
> example, if my XML
> changed to include another level somewhere in there, then I 
> would need to
> re-write my code and recompile.  Am I doing this all wrong?
> 
> Thanks!  :)
> 
> Peter Foti
> Systolic Networks
> Phone:  (xxx) xxx-xxxx
> Fax:  (xxx) xxx-xxxx
> 
> 
> 
> 

  Return to Index