in reply to Re: Trees in XML
in thread Trees in XML

Thanks
I have tried simple and found that it mangles the order of the XML when I put it into a hash. I am also experimenting with LibXML but as I said, I would really like to use XML::Parser tree style.

You see what I would like to do is put the returned value (firstnames) into an array which I can then use later, in another part of the perl script.

So any clues on how to use XML::Parser to obtain th firstnames?

Many thanks

Replies are listed 'Best First'.
Re^3: Trees in XML
by rovf (Priest) on Jun 03, 2008 at 11:49 UTC

    It is not XML::Simple which is mangling the order; it is the very property of a hash, that you don't have order there. That's the reason why I used forcearray in my example: This makes the person elements go into array instead of a hash, and staying in the same order as in the XML file.

    BTW, does the order matter in your example?

    -- 
    Ronald Fischer <ynnor@mm.st>
      I need to keep the parsed file in order as I am parsing out the value of the firstname and using it in a SQL query which then gives me a result which I will place back into the same position as the firstname value. I am aware that this is a complicated procedure and as I said I am a newbie to perl and so I am trying to boil down the solution into smaller, bitesize pieces so that I can learn and understand each step!

      So any clues or explanation would be great

      Thanks

        Is the output you want the same XML document you are reading, with the first name replaced by the result of a query? In that case Tree is probably not the style you want to use. Stream or Subs would be easier to use, at least because they take care of copying the data you don't want to process as is. It still won't be as convenient as a higher-level module though, you will still have to write a bit of code yourself. At least you will have to output back the tags you are processing, and escape the text you want to insert.

        BTW, I have always tried to keep XML::Twig installation as simple as possible, so if you're using perl 5.8.*, you should be able to drop the Twig.pm file from the distribution either in you Perl's library XML directory, or just in a sub-directory named XML of the current directory.