Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

XML::Simple / XMLout Question

by krishnak (Initiate)
on Aug 24, 2017 at 20:24 UTC ( #1197961=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I am trying to build an XML using XMLout option. A flow of what I am doing ..

1. Build the XML string using few If and Else statement using a variable

use XML::Simple qw(XMLout); $samstr = source=>[{abc=>[{uname=>[$VAR1],dbcrdn=>[$VAR2],paridn=>[$V +AR3],osath=>[$VAR4],}] $xmlstr = "{".$samstr."}"; print XMLout($xmlstr,RootName => "root");

using the above steps it is not printing me the correct XML format....

2. Where as if I include the same string like below the XML is generated properly (Please ignore any syntax as I am regenerating the tags here and the program is running fine.)

$xmlstr = { source=>[{abc=>[{uname=>["USER"],dbcrdn=>["PWD"],paridn= +>["XYZ"],osath=>["0"],}] } print XMLout($xmlstr,RootName => "root");

-- Am I missing anything on procedure #1 (top). Appreciate your response very much.

Regards,
Krishna

2017-08-25 Athanasius added code and paragraph tags

Replies are listed 'Best First'.
Re: XML::Simple / XMLout Question
by NetWallah (Canon) on Aug 24, 2017 at 21:57 UTC
    Please see Writeup Formatting Tips and learn how to use <code> tags to post code.

    In attempting to decipher what your code is -- it appears that you have mismatched square-brackets and braces.

    The closest to your code that compiles is:

    $samstr = {source=>[{abc=>[{uname=>$VAR1,dbcrdn=>$VAR2,paridn=>$VAR3,o +sath=>$VAR4,}]}]};
    Give that a try.

    Also : XML::Simple is a terrible module with inconsistent options.

    Consider using an alternative, like XML::Twig.

                    All power corrupts, but we need electricity.

Re: XML::Simple / XMLout Question
by ikegami (Patriarch) on Aug 25, 2017 at 17:09 UTC

    I am trying to build an XML using XMLout option

    That's the first error. While XML::Simple is rather awful for reading XML, it's hopeless for generating XML.

    Alternatives: XML::Writer, XML::LibXML.

Re: XML::Simple / XMLout Question
by poj (Abbot) on Aug 25, 2017 at 05:53 UTC
    Am I missing anything...

    Yes, see eval but this is not the best way to build XML.

    #!/usr/bin/perl use strict; use XML::Simple 'XMLout'; my $samstr = 'source=>[ { abc => [ { uname =>["USER"], dbcrdn=>["PWD"], paridn=>["XYZ"], osath =>["0"], }] }]'; my $xmlstr; eval '$xmlstr = {'.$samstr.'}'; print XMLout($xmlstr,RootName => "root");
    poj
Re: XML::Simple / XMLout Question
by hdb (Monsignor) on Aug 25, 2017 at 07:56 UTC

    XMLout seems to expect a Perl structure not a string, so why don't you build it like this:

    use strict; use warnings; use XML::Simple qw(XMLout); my $VAR1 = "bla1"; my $VAR2 = "bla2"; my $VAR3 = "bla3"; my $VAR4 = "bla4"; my $xml = { source=>[{ abc=>[{ uname=>[$VAR1], dbcrdn=>[$VAR2], paridn=>[$VAR3], osath=>[$VAR4], }] }] }; print XMLout($xml,RootName => "root");

    or more interactively

    use strict; use warnings; use XML::Simple qw(XMLout); my $VAR1 = "bla1"; my $VAR2 = "bla2"; my $VAR3 = "bla3"; my $VAR4 = "bla4"; my $abc; push @$abc, { uname=>[$VAR1], dbcrdn=>[$VAR2], paridn=>[$VAR3], osath= +>[$VAR4], }; my $xml; push @{$xml->{"source"}}, { abc => $abc }; print XMLout($xml,RootName => "root");
Re: XML::Simple / XMLout Question
by Anonymous Monk on Aug 24, 2017 at 21:59 UTC
    Do not use XML::Simple for XML output. Use something like XML::LibXML instead.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2022-06-25 14:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (83 votes). Check out past polls.

    Notices?