Laila has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,
Currently, I'm using Expat to iterate over an XML file, and format each element to certain specs. My experience so far has been that Expat allows you to manipulate elements, but that it doesn't have "knowledge" of the tree structure inherent in XML.

Is there a way to extract an element that is the child (or sibling) of another element? I need to add an item to an array every time a child of a particular element is present/missing.

Thanks a lot!
LL

  • Comment on Using Expat: how to extranct and manipulate elements?

Replies are listed 'Best First'.
Re: Expat
by mirod (Canon) on Mar 13, 2001 at 13:07 UTC

    OK, it's time for the XML exercise of the day.

    Just so you can compare the various programming styles for some of the most common XML modules here are solutions using XML::Parser, XML::Twig, XML::PYX and XML::XPath. You can choose the style you prefer, TMTOWTDI!

Re: Expat
by aardvark (Pilgrim) on Mar 13, 2001 at 09:20 UTC
    For this particular task, I'd look at the XML::XPath modules. XPath allows you to search through a tree and find the nodes you need. The way I understand it, XPath is to XML trees as regular expressions are to strings. The XPath syntax is not very hard to figure out. You can read the specs here.

    Here is an example from the XML::XPath docs

    use XML::XPath; use XML::XPath::XMLParser; my $xp = XML::XPath->new(filename => 'test.xhtml'); my $nodeset = $xp->find('/html/body/p'); # find all paragraphs foreach my $node ($nodeset->get_nodelist) { print "FOUND\n\n", XML::XPath::XMLParser::as_string($node), "\n\n"; }

    I think XPath is really interesting stuff and if you post some of the xml you are dissecting, I'll try to help you out as best I can.

    You might also want to give the perl-xml mailing list a quick search.

    Get Strong Together!!

Re: Expat
by yakko (Friar) on Mar 13, 2001 at 08:44 UTC
    I haven't had much XML experience, but looking at XML::Twig's man page on CPAN, it probably would do this. The author will most likely be by to club me over the head if I'm wrong, however. There are probably other modules in the XML class that will help as well.

    --
    Me spell chucker work grate. Need grandma chicken.