How do I get rid of unwanted nodes returned by findnodes from Perl's XML::LibXML module? -
following small fraction of xml working on. want extract attributes, tag name , texts under substree.
<?xml version='1.0' encoding='utf-8'?> <warehouse> <equipment id="abc001" model="tv" version="3_00"> <attributes> <location>chicago</location> <latitude>30.970</latitude> <longitude>-90.723</longitude> </attributes> </equipment></warehouse>
i have coded example this:
#!/usr/bin/perl use xml::libxml; use data::dumper; $parser = xml::libxml->new(); $chunk = $parser->parse_file("numone.xml"); @equipment = $chunk->findnodes('//equipment'); foreach $at ($equipment[0]->getattributes()) { ($na,$nv) = ($at -> getname(),$at -> getvalue()); print "$na => $nv\n"; } @equipment = $chunk->findnodes('//equipment/attributes'); @attr = $equipment[0]->childnodes; print dumper(@attr); foreach $at (@attr) { ($na,$nv) = ($at->nodename, $at->textcontent); print "$na => $nv\n"; }
i getting results this:
id => abc001 model => tv version => 3_00 $var1 = bless( do{\(my $o = 10579528)}, 'xml::libxml::text' ); $var2 = bless( do{\(my $o = 13643928)}, 'xml::libxml::element' ); $var3 = bless( do{\(my $o = 13657192)}, 'xml::libxml::text' ); $var4 = bless( do{\(my $o = 13011432)}, 'xml::libxml::element' ); $var5 = bless( do{\(my $o = 10579752)}, 'xml::libxml::text' ); $var6 = bless( do{\(my $o = 10565696)}, 'xml::libxml::element' ); $var7 = bless( do{\(my $o = 13046400)}, 'xml::libxml::text' ); #text => location => chicago #text => latitude => 30.970 #text => longitude => -90.723 #text =>
extract attributes seem ok, extracting tag name , text got contents. questions are:
- where
::text
element came from? - how rid of elements ,
#text
things?
thanks,
first of should use strict
, use warnings
@ start of program, , declare variables @ point of first use my
. reveal lot of simple mistakes , important in programs asking with.
as have been told, xml::libxml::text
entries whitespace text nodes. if want xml::libxml
parser ignore then set no_blanks
option on parser object.
also, better off using more recent load_xml
method instead of outdated parse_file
below
my $parser = xml::libxml->new(no_blanks => 1); $chunk = $parser->load_xml(location => "numone.xml");
the output changed version of program looks like
id => abc001 model => tv version => 3_00 $var1 = bless( do{\(my $o = 7008120)}, 'xml::libxml::element' ); $var2 = bless( do{\(my $o = 7008504)}, 'xml::libxml::element' ); $var3 = bless( do{\(my $o = 7008144)}, 'xml::libxml::element' ); location => chicago latitude => 30.970 longitude => -90.723
Comments
Post a Comment