in reply to Re^3: Parsing XML
in thread Parsing XML

Hi, this is what I am getting:

#!/usr/bin/perl use strict; use warnings; use XML::Twig; use Data::Dumper; my $test_xml = qq|<?xml version="1.0" encoding="ISO-8859-1"?> <CustDetails> <CustName>Unix</CustName> <CustomerId>999</CustomerId> <Age>28</Age> </CustDetails> |; my $t= XML::Twig->new( twig_handlers => { 'CustDetails' => sub { $_->first_child('C +ustomerId')->text(), } } ) ->parse($test_xml); warn Dumper $t; # I am trying to get this: 999 # in instead, I am getting this: =code $VAR1 = bless( { 'twig_xmldecl' => { 'version' => '1.0', 'standalone' => undef, 'encoding' => 'ISO-8859-1' }, 'twig_keep_pi' => 1, 'twig_current' => undef, 'twig_autoflush' => 1, 'twig_space_policy' => { 'CustDetails' => 0 }, 'twig_entity_list' => bless( { 'updated' => 0, 'entities' => {} }, 'XML::Twig::Entity_list +' ), 'twig_do_not_escape_amp_in_atts' => 0, 'twig' => $VAR1, 'NoExpand' => 0, 'twig_keep_comments' => 1, 'twig_discard_spaces' => 1, '_twig_context_stack' => undef, 'twig_elt_class' => 'XML::Twig::Elt', 'twig_id' => 'id', 'twig_parsing' => undef, 'Non_Expat_Options' => { 'NoLWP' => 1, 'Non_Expat_Options' => 1, '_HNDL_TYPES' => 1, 'Handlers' => 1, 'Style' => 1 }, 'Handlers' => { 'CdataEnd' => sub { "DUMMY" }, 'Default' => sub { "DUMMY" }, 'CdataStart' => sub { "DUMMY" }, 'Start' => sub { "DUMMY" }, 'Comment' => sub { "DUMMY" }, 'Entity' => sub { "DUMMY" }, 'End' => sub { "DUMMY" }, 'Final' => sub { "DUMMY" }, 'Doctype' => sub { "DUMMY" }, 'Char' => sub { "DUMMY" }, 'Init' => sub { "DUMMY" }, 'Attlist' => sub { "DUMMY" }, 'Element' => sub { "DUMMY" }, 'ExternEnt' => sub { "DUMMY" }, 'XMLDecl' => sub { "DUMMY" }, 'Proc' => sub { "DUMMY" } }, 'twig_ext_ent_handler' => sub { "DUMMY" }, 'trailing_spaces' => ' ', 'twig_handlers' => { 'xpath_handler' => { 'Age' => undef, 'CustomerId' => + undef, 'CustName' => u +ndef, '*' => undef, 'CustDetails' = +> [ + { + 'trigger' => sub { "DUMMY" }, + 'handler' => sub { "DUMMY" }, + 'test_on_text' => 0, + 'path' => 'CustDetails', + 'score' => { + 'steps' => 1, + 'type' => 3, + 'anchored' => 0 + }, + 'tag' => 'CustDetails' + } +] }, 'handlers' => { 'string' => { 'CustDet +ails' => $VAR1->{'twig_handlers'}{'xpath_handler'}{'CustDetails'}[0]{ +'handler'} } } }, 'TwigHandlers' => { 'CustDetails' => $VAR1->{'twig_ha +ndlers'}{'xpath_handler'}{'CustDetails'}[0]{'handler'} }, 'extra_data' => '', 'Pkg' => 'XML::Twig', '_HNDL_TYPES' => { 'CdataEnd' => sub { "DUMMY" }, 'Start' => sub { "DUMMY" }, 'Entity' => sub { "DUMMY" }, 'ExternEntFin' => sub { "DUMMY" }, 'End' => sub { "DUMMY" }, 'Final' => 1, 'Doctype' => sub { "DUMMY" }, 'Char' => sub { "DUMMY" }, 'Init' => 1, 'XMLDecl' => sub { "DUMMY" }, 'Default' => sub { "DUMMY" }, 'CdataStart' => sub { "DUMMY" }, 'Comment' => sub { "DUMMY" }, 'Unparsed' => sub { "DUMMY" }, 'ExternEnt' => sub { "DUMMY" }, 'Element' => sub { "DUMMY" }, 'Attlist' => sub { "DUMMY" }, 'DoctypeFin' => sub { "DUMMY" }, 'Notation' => sub { "DUMMY" }, 'Proc' => sub { "DUMMY" } }, 'twig_keep_encoding' => 0, 'twig_dtd_handler' => undef, 'twig_in_pcdata' => 0, 'twig_parser' => undef, 'twig_stored_spaces' => ' ', 'twig_root' => bless( { 'gi' => '6', 'first_child' => bless( { 'next_s +ibling' => bless( { + 'next_sibling' => bless( { + 'parent' => $VAR1->{'twig_ +root'}, + 'gi' => '9', + 'prev_sibling' => $VAR1->{ +'twig_root'}{'first_child'}{'next_sibling'}, + 'first_child' => bless( { + 'p +arent' => $VAR1->{'twig_root'}{'first_child'}{'next_sibling'}{'next_s +ibling'}, + 'g +i' => 1, + 'p +cdata' => '28' + }, ' +XML::Twig::Elt' ), + 'att' => {}, + 'last_child' => $VAR1->{'t +wig_root'}{'first_child'}{'next_sibling'}{'next_sibling'}{'first_chil +d'} + }, 'XML::Twig::Elt' ), + 'parent' => $VAR1->{'twig_root'}, + 'gi' => '8', + 'prev_sibling' => $VAR1->{'twig_root'}{'first_child +'}, + 'first_child' => bless( { + 'parent' => $VAR1->{'twig_r +oot'}{'first_child'}{'next_sibling'}, + 'gi' => 1, + 'pcdata' => '999' + }, 'XML::Twig::Elt' ), + 'att' => {}, + 'last_child' => $VAR1->{'twig_root'}{'first_child'} +{'next_sibling'}{'first_child'} + }, 'XML::Twig::Elt' ), 'parent +' => $VAR1->{'twig_root'}, 'gi' => + '7', 'first_ +child' => bless( { + 'parent' => $VAR1->{'twig_root'}{'first_child'}, + 'gi' => 1, + 'pcdata' => 'Unix' + }, 'XML::Twig::Elt' ), 'att' = +> {}, 'last_c +hild' => $VAR1->{'twig_root'}{'first_child'}{'first_child'} }, 'XML:: +Twig::Elt' ), 'att' => {}, 'last_child' => $VAR1->{'twig_r +oot'}{'first_child'}{'next_sibling'}{'next_sibling'}, 'twig' => $VAR1 }, 'XML::Twig::Elt' ) }, 'XML::Twig' ); =cut
}, DUMMYparent

Replies are listed 'Best First'.
Re^5: Parsing XML
by Corion (Patriarch) on Sep 02, 2016 at 17:36 UTC

    That's the output you get from

    warn Dumper $t;

    Why do you do that when you don't know what happens there?

    Maybe you want to store or print a value when XML::Twig invokes the callback for your node, in the following callback?

    'CustDetails' => sub { $_->first_child('CustomerId')->text(), }

    Maybe something like the following:

    'CustDetails' => sub { print "The first customer has the following det +ails: " . $_->first_child('CustomerId')->text(), }