in reply to Re: Parsing XML
in thread Parsing XML

Using "->parse(...)" got me this "<CustomerId>999</CustomerId>"

Is there a way to get just the value "999" directly?

Thanks!

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

    Yes, and that way is documented in the XML::Twig documentation. Maybe using ->print is the wrong method here. Most likely, you will find the appropriate method to get at the text of a node in the documentation.

      The documentation isn't very friendly, I trying this way, but I am getting a lot of XML instead of just the "999":
      ... my $t= XML::Twig->new( twig_handlers => { 'CustDetails' => sub { $_->first_child('C +ustomerId')->text(), } } ) ->parse($test_xml); warn Dumper $t;

        Have you tried searching for the "text" or "content" method? Maybe it does what you need?

        You don't show what output you actually get, and I'm really bad at guessing.

        Maybe you can show what output you get for which input.

        Personally, I found it always quite enlightening to also look at the value of ->path to see which node is output, and also maybe find out why.

      Yeah, this worked:
      my $val; my $t= XML::Twig->new( twig_handlers => { 'CustDetails' => sub { $val = $_->first_child('Custome +rId')->text(), } } ) ->parse($test_xml); print $val;

      I wish it was less complicated for such a simple example on how to use XML::Twig - Could it be simpler just using RegExp?

      Thank you!
        I wish it was less complicated for such a simple example on how to use XML::Twig - Could it be simpler just using RegExp?

        A simple example may simply hide the tangled inherent complexity of XML. Heed the wise advice of Corion. Resist the temptation to use regexes; that way lies madness!


        Give a man a fish:  <%-{-{-{-<

      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

        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(), }