in reply to Any help available for a newbie to XML::LibXML?

I started with a couple other XML packages way back when and was never happy. I accidentally discovered LibXML and after fighting through the docs (they actually are pretty good, you just have to read them all front to back and then use them like a function index) I've never been happier. So stick it out, as it were. My Xpath sucks but here's something anyway.

First, there's a good basic tutorial here: Xpath basics. I use that site over and over because their tutorials are straightforward and well organized even when they aren't the deepest/best. Then, this is what I tried just to play around.

use XML::LibXML; my $parser = XML::LibXML->new(); my $tree = $parser->parse_fh(*DATA); my $root = $tree->getDocumentElement; for my $node ( $root->findnodes("//Device/*/status/text()") ) { print $node->nodeValue, $/; } # print $root->serialize(1); __DATA__ <?xml version="1.0" standalone="yes" ?> <SymCLI_ML> <Symmetrix> <Symm_Info> <symid>000290101935</symid> </Symm_Info> <Device> <Dev_Info> <pd_name>Not Visible</pd_name> <dev_name>0040</dev_name> # ...

To iterate through all the Device childNodes, this sort of thing should be pretty fast. I think there is a native Xpath for getting child nodes too.

for my $node ( $root->findnodes("//Device/*") ) { print $node->nodeName, $/; }

You could certainly cook up a hash of the nodeNames/Xpath to "your key names" you want and run your Xpath queries with it so that your solution would (human) scale as it would be little more than a configuration based filter.

(update: corrected an extra word and code comment)

Replies are listed 'Best First'.
Re^2: Any help available for a newbie to XML::LibXML?
by wardy3 (Scribe) on Feb 29, 2008 at 22:32 UTC

    Thanks for the reply

    You've given me some things to think about :-) I'll have a play next week at work

Re^2: Any help available for a newbie to XML::LibXML?
by wardy3 (Scribe) on Mar 03, 2008 at 08:39 UTC
    Hi again

    I've written a bit more code to test it out and it is very slow doing the findnodes when I'm already at a position in the tree.

    I've written this test script:

    my $parser = XML::LibXML->new(); my $doc = $parser->parse_file($filename); my @devinfo = $doc->findnodes('//Device/Dev_Info'); foreach my $devinfo (@devinfo) { my($dev) = $devinfo->findnodes('./dev_name'); print $dev->to_literal, "\n"; }

    The first findnodes runs very quickly (< 1 sec) but the one in the loop is a lot slower. But I don't know why. Doesn't it just have to examine the nodes "under" the current devinfo tag?

    Am I doing something wrong with the findnodes call here?

    Thanks, ~ Michael

      The only thing that I see from looking at your code that might be responsible would be that

      my($dev) = $devinfo->findnodes('./dev_name');

      actually finds many nodes just to throw them all away except for the first. Maybe do it this way to check how many nodes you find:

      my @found_devices = $devinfo->findnodes('./dev_name'); warn "Found " . scalar(@found_devices); my $dev = $found_devices[0];

      But that's just a shot in the dark. Actually looking at your XML, it shouldn't find more than one device.

        Thanks Corion - interesting thought.

        but unfortunately it's only finding 1 at a time. The speed is about 2 per second which is quite atrocious.

        And I agree, it should only find 1 too. I'm guessing there's a fixed overhead for findnodes which would make sense for what I'm seeing

        Anyway, I've got a few more replies to try out :-)
        and maybe it is time to re-visit my old friend XSLT

        Thanks again for the reply - I'll post back any more findings