Forum Moderators: coopster & phranque

Message Too Old, No Replies

Complex data structures

         

Robber

6:52 pm on Dec 16, 2002 (gmt 0)

10+ Year Member



I have an xml document (yes another one! - I have book in the post so might stop asking soon!)

I've used XML::Parser to output as a Tree, now assuming: $$tree[1]->[4]->[0]->{"name"};

gives me the name of a category and

$$tree[1]->[8]->[0]->{"name"};

gives me the name of the next category, how would I go about traversing my tree so I can do a foreach on each category?

The actual data I want is pulled out along the lines of
$$tree[1]->[4]->[4]->[4]->[24]->[2];

but obviously this will only do it for a single category and I need to iterate over all of them.

I was introduced to map a couple of weeks ago on a much simpler question, but I cant see how that would do the trick here. Any suggestions?

Cheers

andreasfriedrich

7:05 pm on Dec 16, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To traverse trees like these you need a function which calls itself recursively for the children of an element.

But why not use XML::XPath [search.cpan.org] or XML::TreeBuilder [search.cpan.org]?

Andreas

Robber

7:09 pm on Dec 16, 2002 (gmt 0)

10+ Year Member



Hi Andreas,

I was hoping you might drop by. I have used a bit of XPath before so it could be a good bet, although I haven't used it in Perl, whats the module like to use?

I am going to be working with some pretty large xml documents - will it handle these OK. I haven't come across TreeBuilder before so I will take a look at it now.

Thanks

Robber

7:21 pm on Dec 16, 2002 (gmt 0)

10+ Year Member



XPath is looking like a goo bet - just installed it and had a quick play and it seems pretty simple, good tip, cheers.