Perl, XML::Twig, how to reading field with the same tag -
i'm working on processing xml file receive partner. not have influence on changing makeup of xml file. extract of xml is:
<?xml version="1.0" encoding="utf-8"?> <objects> <object> <id>vw-xjc9</id> <name>name</name> <type>house</type> <description> <![cdata[<p>some descrioption of house</p>]]> </description> <localcosts> <localcost> <type>mandatory</type> <name>what kind of cost</name> <description> <![cdata[some text again, different first tag]]> </description> </localcost> </localcosts> </object> </objects>
the reason use twig xml 11gb big, 100000 different objects) . problem when reach localcosts part, 3 fields (type, name , description) skipped, because these names used before.
the code use go through xml file follows:
my $twig= new xml::twig( twig_handlers => { id => \&get_id, name => \&get_name, type => \&get_type, description => \&get_description, localcosts => \&get_localcosts }); $lokaal="c:\\temp\\data3.xml"; getstore($xml, $lokaal); $twig->parsefile("$lokaal"); sub get_id { my( $twig, $data)= @_; $field[0]=$data->text; $twig->purge; } sub get_name { my( $twig, $data)= @_; $field[1]=$data->text; $twig->purge; } sub get_type { my( $twig, $data)= @_; $field[3]=$data->text; $twig->purge; } sub get_description { my( $twig, $data)= @_; $field[8]=$data->text; $twig->purge; } sub get_localcosts{ ($t, $item) = @_; @localcosts = $item->children; $localcost ( @localcosts ) { print "$field[0]: $localcost->text\n"; @costs = $localcost->children; $cost (@costs) { $type =$cost->text if $cost->name eq q{type}; $name =$cost->text if $cost->name eq q{name}; $description=$cost->text if $cost->name eq q{description}; print "fields: $type, $name, $description\n"; } } $t->purge; }
when run code, main fields read without issues, when code arrives @ 'localcosts' part, second for-next loop not executed. when change field names in xml unique ones, code works perfectly.
can me out?
thanks
if want handlers type, name , desctiption triggered in object tag, specify path:
my $twig = new xml::twig( twig_handlers => { id => \&get_id, 'object/name' => \&get_name, 'object/type' => \&get_type, 'object/description' => \&get_description, localcosts => \&get_localcosts });
Comments
Post a Comment