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 (Patriarch) 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