persistence is futile

use the twiggyness of XML::LibXML::Reader

#!/usr/bin/perl -- use strict; use warnings; use Path::Tiny qw/ path /; use XML::LibXML::Reader; my $deleteme = 'deleteme.xml'; my $xml = q{<root><product> <name>5 Spoke Wheel</name> <description>Reconditioned OEM</description> <price>123.45</price> <buyurl>http://foo.example.com</buyurl> <imageurl>http://foo.example.com/foo.jpg</imageurl> </product><product> <name>sixexample</name> <description>six example</description> <price>66666.66</price> <buyurl>http://six.example.com</buyurl> <imageurl>http://six.example.com/foo.jpg</imageurl> </product><product> <name>two</name> <description>two</description> <price>222.45</price> <buyurl>http://two.example.com</buyurl> <imageurl>http://two.example.com/foo.jpg</imageurl> </product></root>}; path( $deleteme )->spew_raw( $xml ); my $reader = XML::LibXML::Reader->new(location => $deleteme ) or die "cannot read file.xml\n"; my $pattern = XML::LibXML::Pattern->new('/root/product'); while($reader->nextPatternMatch( $pattern) ) { my $node = $reader->copyCurrentNode(!!'deep'); ## get the twig next if ! $node ->hasChildNodes; ## skip empty like closing tags processNode( $node ) ; } $reader->close; undef $reader; path( $deleteme )->remove; exit( 0 ); sub processNode { my( $product ) = @_; my $price = $product->F('./price/text()'); my $name = $product->F('./name/text()'); my $imageurl = $product->F('./imageurl/text()'); print "$price $name $imageurl\n\n"; } sub XML::LibXML::Node::F { my $self = shift; my $xpath = shift; my %prefix = @_; our $XPATHCONTEXT; $XPATHCONTEXT ||= XML::LibXML::XPathContext->new(); while( my( $p, $u ) = each %prefix ){ $XPATHCONTEXT->registerNs( $p, $u ); } $XPATHCONTEXT->findnodes( $xpath, $self ); } __END__ 123.45 5 Spoke Wheel http://foo.example.com/foo.jpg 66666.66 sixexample http://six.example.com/foo.jpg 222.45 two http://two.example.com/foo.jpg

In reply to Re: Assigning variables and persistence using Lib::LibXML::Reader (twig) by Anonymous Monk
in thread Assigning variables and persistence using Lib::LibXML::Reader by CA_Tallguy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.