Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best way to process XML?

Status
Not open for further replies.

Zippeh

Programmer
Sep 24, 2002
56
GB
Hi, I got an XML file. The basic structure is something liek this:
Code:
batches
  batch
    transactions
      transaction
        allocations
          allocation
          allocation
          allocation
      transaction
        allocations
          allocation
      transaction
        allocations
          allocation
  batch
    transactions
      transaction
        allocations
          allocation
          allocation

I've decided to use XML::DOM for processing. The code I have is something like this in pseudo code:

Code:
  Find all batches
    Foreach batch
      Find all transactions
        Foreach transaction
          Find all allocations
        End
    End

So I'm going to have lots of nested loops. Is there a better way of doing this using the XML::DOM? I just think it looks really untidy!
 
There are two quite different approaches to dealing with XML. The DOM approach involves parsing the document into a data structure and then navigating the structure.

The second approach involves registering callback handlers on different tags. The call-backs themselves have access to the parent section of the tree so they can find out about their containing tags.

This approach avoids the nested loops you describe and doesn't require the whole document to be in memory. You would attach a callback to <allocations> that would be fired repeatedly and each time could look "up" to it's containing transaction and batch.

It's a bit of a mental gear shift if you are not used to it - a bit like the change from writing character-driven apps to windows-based ones - but it's worth a little exploration to see if it suits.

XML::parser takes this approach.

f

&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top