Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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' } ] ] ];

Replies are listed 'Best First'.
Re: What may be the problem in my code?
by monkey_boy (Priest) 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>

    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 (Abbot) 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?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2016-09-27 07:55 GMT
Find Nodes?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:

    Results (499 votes). Check out past polls.