Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Parsing XML

by Anonymous Monk
on Sep 02, 2016 at 15:37 UTC ( #1171065=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

I have this test script trying to use XML::Twig to extract as an example the "CustomerId" from the XML. I can not understand why I am getting "Couldn't open" - "No such file or directory" erros when the file if right there.
Here is the sample code and thanks for helping!
#!/usr/bin/perl use strict; use warnings; use XML::Twig; use Data::Dumper; my $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/CustomerId' => sub { $_->pri +nt } } ) ->parsefile($xml); warn Dumper $t;

Replies are listed 'Best First'.
Re: Parsing XML
by Corion (Pope) on Sep 02, 2016 at 15:40 UTC
    ->parsefile

    expects a filename, but you're passing it raw XML already.

    Most likely,

    ->parse(...)

    will work better for you...

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

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

      Thanks!

        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.

Re: Parsing XML
by neilwatson (Priest) on Sep 02, 2016 at 15:40 UTC

    It says parsefile not parsestring. A file is expected.

    Neil Watson
    watson-wilson.ca

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1171065]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2021-10-25 20:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (90 votes). Check out past polls.

    Notices?