Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: XML::Simple, XMLout and encoding

by liverpole (Monsignor)
on Mar 02, 2010 at 00:15 UTC ( #826031=note: print w/replies, xml ) Need Help??

in reply to XML::Simple, XMLout and encoding

Hi horrendo, I haven't done anything with XML before, but my recent work project will require it, so I used your question to "get my feet wet".

Take a look at the XML::Simple documentation (hint: search for "xml version="), and you'll find:

XMLDecl => 1 or XMLDecl => 'string' *# out - handy* If you want the output from "XMLout()" to start with the optional +XML declaration, simply set the option to '1'. The default XML declara +tion is: <?xml version='1.0' standalone='yes'?> If you want some other string (for example to declare an encoding value), set the value of this option to the complete string you re +quire.

That (plus the first couple of lines of the SYNOPSIS) quickly let me arrive at the following code sample, all within a few minutes:

use strict; use warnings; use XML::Simple; my $h_ref = { 'a' => 1, 'b' => 2, 'c' => 3, 'colors' => [ 'red', 'green', 'blue' ], 'sub_hash' => { 'numbers' => [ 0, 1, 2], 'letters' => { 'vowels' => [qw[a e i o u]], }, }, }; my $ref = "1,2,3"; my $xs = XML::Simple->new(ForceArray => 1, KeepRoot => 1, XMLDecl => +1); my $xml = $xs->XMLout($h_ref); print "XML is:\n$xml\n"; __END__ [Output] <?xml version='1.0' standalone='yes'?> <opt a="1" b="2" c="3"> <colors>red</colors> <colors>green</colors> <colors>blue</colors> <sub_hash> <letters> <vowels>a</vowels> <vowels>e</vowels> <vowels>i</vowels> <vowels>o</vowels> <vowels>u</vowels> </letters> <numbers>0</numbers> <numbers>1</numbers> <numbers>2</numbers> </sub_hash> </opt>

Update:  Made search string more specific.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://826031]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2017-10-22 21:21 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (275 votes). Check out past polls.