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

Hello, New to XML and need to use it, I know... Here is a sample of my XML:
<AvailableBatch> <Date>08/31/2004</Date> <Available> <Sku>40</Sku> <Part>10 </Part> <Location>20</Location> <Qty>1</Qty> <Time>18:38:01</Time> </Available> <Available> <Sku>40</Sku> <Part>10 </Part> <Location>60</Location> <Qty>0</Qty> <Time>18:38:01</Time> </Available> </AvailableBatch>

I need to treat each <Available> grouping as a record and get the <Part> and <Qty> for each, then test each record against a MySQL table.

Below is attempt to try and use XML::Path, some code I gleaned from Perl / XML.

I can see how to get one element, but I could use a hint at how to group the child elements. The samples I see all count of the elements having attributes, which my file does not use.

Thanks, I hope I'm not too far to the "help me write me script" side, I just need a good hint or two.

use XML::XPath; my $file = 'available_batchnyyn.xml'; my $xp = XML::XPath->new(filename=>$file); my $nodeset = $xp->find('//Sku'); my @zipcodes; # Where we'll put our results if (my @nodelist = $nodeset->get_nodelist) { @zipcodes = map($_->string_value, @nodelist); @zipcodes = sort(@zipcodes); local $" = "\n"; print "I found these zipcodes:\n@zipcodes\n"; } else { print "The file $file didn't have any 'zip' elements in it!\n"; }
-dstefani

Replies are listed 'Best First'.
Re: XML::XPath tricks and tips
by mirod (Canon) on Sep 02, 2004 at 00:18 UTC

    I can't really see what your code is supposed to do, what's with the zipcodes?

    Here is an example of how to get data from a file using XML::XPath:

    #!/usr/bin/perl -w use strict; use XML::XPath; my $file = 'available_batchnyyn.xml'; my $xp = XML::XPath->new(filename=>$file); # /AvailableBatch/Available is faster than //Available my @available = $xp->findnodes('/AvailableBatch/Available'); foreach my $available (@available) { my $part= $available->findvalue( './Part[1]'); # the [1] is not re +ally needed my $qty= $available->findvalue( './Qty[1]'); $part=~ s{\s+$}{}; # you might need to + trim all values print "part $part - qty: $qty\n"; }
Re: XML::XPath tricks and tips
by Aristotle (Chancellor) on Sep 02, 2004 at 00:16 UTC

    Disclaimer: I've never used XML::XPath and don't even have it installed. I'm writing this from a quick glance at the documentation and my experience working with XML::LibXML. You should get the idea.

    for my $available ( $xp->findnodes( '//Available' ) ) { my $qty = $available->findvalue( 'Qty' ); my $part = $available->findvalue( 'Part' ); # ... }

    The important part is to realize that the XPath expressions passed to $available->findvalue() will be evaluated relative to the node $available represents.

    Makeshifts last the longest.

Re: XML::XPath tricks and tips
by johnnywang (Priest) on Sep 02, 2004 at 00:26 UTC
    A similar, actually more complex, question was asked in Help with XML::XPath, where I gave a few code samples.
Re: XML::XPath tricks and tips
by gmpassos (Priest) on Sep 02, 2004 at 16:03 UTC
    If you want to just access XML values you don't need to use XPath to find this informations. You can use it, but the XPath implementation in Perl (XML::XPath), in my opinion, is not very intuitive to use.

    What you can do is to use some module that loads XML into a HASH tree, and access the values directly from the tree. You can take a look at XML::Simple (more popular) and XML::Smart (well, I'm the author of it, so I shouldn't say what is better).

    Well, the main idea of XML::Smart is to enable the use of XML by programmers without need to really know the XML format. Here's a simple example of how to do what you want:

    use XML::Smart ; my $xml = new XML::Smart('available_batchnyyn.xml'); my @avaliables = @{ $xml->{AvailableBatch}{Available} } ; foreach my $avaliables_i ( @avaliables ) { my $part = $avaliables_i->{Part} ; my $qty = $avaliables_i->{Qty} ; print "Part: $part ; Qty: $qty\n" ; }

    Graciliano M. P.
    "Creativity is the expression of the liberty".