Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

What may be the problem in my code?

by gube (Parson)
on Jan 06, 2006 at 14:23 UTC ( #521481=perlquestion: print w/ replies, xml ) Need Help??
gube has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I am taking input $text and parse using XML::Simple and XML::Parser but, in XML::Simple the element 'a' is not showned in parsed output. It, seems in XML::Parser. What may be the changes in my code in XML::Simple. Please help me.


Thanks in Advance
use XML::Simple; use Data::Dumper; $text = qq{<a b="1" c="1"><b c="1"/></a>}; my $res = XML::Simple->new(); $result = $res->XMLin( $text ); print Dumper($result); o/p: $VAR1 = { 'c' => '1', 'b' => [ '1', { 'c' => '1' } ] }; ------------- use XML::Parser; $parser = new XML::Parser( Style => 'Tree' ); my $tree = $parser->parse( $text ); use Data::Dumper; print Dumper( $tree ); o/p: $VAR1 = [ 'a', [ { 'c' => '1', 'b' => '1' }, 'b', [ { 'c' => '1' } ] ] ];

Comment on What may be the problem in my code?
Download Code
Re: What may be the problem in my code?
by monkey_boy (Curate) on Jan 06, 2006 at 14:40 UTC
    it is taking "a" as your "root" node, try adding a fake "root" tag, e.g.
    <root><a b="1" c="1"><b c="1"/></a></root>

    UPDATE:
    or add this option:
    my $res = XML::Simple->new(KeepRoot => 1);


    This is not a Signature...
      Thanks ;)
Re: What may be the problem in my code?
by Happy-the-monk (Monsignor) on Jan 06, 2006 at 14:43 UTC

    Your A-tag is the outer structure that XML::Simple uses. If you add any other element to it, as an axample put <xml> ... </xml> around your structure, you will see the A-tag.

    Cheers, Sören

Re: What may be the problem in my code?
by ptum (Priest) on Jan 06, 2006 at 14:48 UTC

    Looking at the documentation for XML::Simple it looks to me as though this is a feature, not a bug. It seems that XML::Simple strips off the name-value pairs and returns them without the name of the tag. Taken from the EXAMPLES section of the POD for XML::Simple:

    When XMLin() reads the following very simple piece of XML: <opt username="testuser" password="frodo"></opt> it returns the following data structure: { 'username' => 'testuser', 'password' => 'frodo' }

    It seems that XML::Simple expects you to use a named variable to hold the contents of the data structure, like this:

    use XML::Simple; my $res = XML::Simple->new() my $a_tag = $res->XMLin($text);

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2014-08-23 07:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (172 votes), past polls