Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

force elements in XML::Smart

by Errto (Vicar)
on Apr 23, 2007 at 23:01 UTC ( [id://611614]=perlquestion: print w/replies, xml ) Need Help??

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

I was looking for a quick way to spit out XML from a data structure and came across XML::Smart. Nifty module. But I'm running into a stumbling block, which is that it is giving me attributes where I would like separate elements. My code is like this:
my $xml = XML::Smart->new; $xml->{Body} = { Foo => 'Bar' }; $xml->save("test.xml");
and it is producing this output:
<Body Foo="Bar"/>
whereas I would like it to produce this output:
<Body><Foo>Bar</Foo></Body>
and was hoping there was a straightforward way to do so. Cheers,

Replies are listed 'Best First'.
Re: force elements in XML::Smart
by GrandFather (Saint) on Apr 24, 2007 at 00:15 UTC

    XML::Smart seems just a bit too clever at times. You may like XML::Maker a bit better:

    use strict; use warnings; use XML::Maker; my $xml = XML::Maker->new ('Body'); my $foo = $xml->subtag ('Foo'); $foo->subtag ('Bar'); print $xml->make ();

    Prints:

    <Body><Foo>Bar</Foo></Body>

    or an interesting alternative is to use XML::Spew:

    use strict; use warnings; package My::Spew; use XML::Spew; use base 'XML::Spew'; __PACKAGE__->_tags(qw/Body Foo/); package main; my $spew = My::Spew->_new (); print $spew->Body ($spew->Foo ('Bar'));

    which generates the same output.


    DWIM is Perl's answer to Gödel
Re: force elements in XML::Smart
by almut (Canon) on Apr 23, 2007 at 23:47 UTC

    According to the docs, the following should force it to be a node:

    $xml->{Body} = { Foo => 'Bar' }; $xml->{Body}{Foo}->set_node(1);

    Otherwise, there are heuristics like there being newlines (\n) in the content, that determine whether a node or an attribute is being created...  Cheers.

Re: force elements in XML::Smart
by gube (Parson) on Apr 24, 2007 at 03:39 UTC

    Hi

    You can use XML::Simple module it will be simple. To parse xml or to create xml you can simply use XML::Simple.
    use strict; use warnings; use XML::Simple; use Data::Dumper; my $xml_input = qq{<Body><Foo>Bar</Foo></Body>}; my $xml_simple = XML::Simple->new(KeepRoot => 1, KeyAttr => 1, ForceAr +ray => 1); my $result = $xml_simple->XMLin($xml_input); print "\nParsed xml with output as reference..", Dumper($result); my $result_xml = $xml_simple->XMLout($result); print "\nConstructed xml using reference..", $result_xml;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-19 21:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found