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

Re^2: Which XML module to use for this scenario?

by Perllace (Acolyte)
on Apr 23, 2011 at 06:57 UTC ( [id://900931]=note: print w/replies, xml ) Need Help??


in reply to Re: Which XML module to use for this scenario?
in thread Which XML module to use for this scenario?

Thanks wind, Please have a look at this
use XML::Simple; use strict; use warnings; my ($id, $value); #group 1 paramters $id = "ID1"; $value = "Recording INformation"; getParameter($id, $value); $id = "ID2"; $value = "Pausing INformation Record "; getParameter($id, $value); $value = "Information Loading"; getParameter($id, $value); $value = "Information loading Complete"; #From here group 2 parameter starts my $id = 2; $value = "Picture loading"; getParameter($id, $value); my $id = 3; $value = "Picture loading complete"; getParameter($id, $value); #Group 2 parameters end. sub getParameter { my ( $id,$value) = @_; my $hash = { 'Key' => $id, 'Value' => $value }; my $xml = { Parameter => [] }; push( @{ $xml->{Parameter} } , { key => $hash->{Key}, content => $hash->{Value} } ); print XMLout( $xml, RootName => 'Parameter' ); }

Right now, the output that i get is something like
<Parameter key="ID1">Recording INformation</Parameter> <Parameter key="ID2">Pausing INformation Record</Parameter> <Parameter key="ID2">Information Loading</Parameter> <Parameter key="ID2">Information loading Complete</Parameter>
If I need something like
<Parameters> - <ParameterGroup ID="Group1"> <Parameter key="Key1">Some Value</Parameter> <Parameter key="Key2">Some Value</Parameter> <Parameter key="Key3">Some Value</Parameter> </ParameterGroup> - <ParameterGroup ID="Group2"> <Parameter key="Key1">Some Value</Parameter> <Parameter key="Key2">Some Value</Parameter> <Parameter key="Key3">Some Value</Parameter> </ParameterGroup> </Parameters>
What do I have to do? Thanks

Replies are listed 'Best First'.
Re^3: Which XML module to use for this scenario?
by wind (Priest) on Apr 23, 2011 at 07:42 UTC

    To get XML of a specific format, work backwards. Start with xml exactly how you like it and see what type of data structure it makes:

    #!/usr/bin/perl use Data::Dumper; use XML::Simple; use strict; use warnings; my $data = do {local $/; <DATA>}; my $xml = XMLin($data); print Dumper($xml); __DATA__ <Parameters> <ParameterGroup ID="Group1"> <Parameter key="Key1">Some Value</Parameter> <Parameter key="Key2">Some Value</Parameter> <Parameter key="Key3">Some Value</Parameter> </ParameterGroup> <ParameterGroup ID="Group2"> <Parameter key="Key1">Some Value</Parameter> <Parameter key="Key2">Some Value</Parameter> <Parameter key="Key3">Some Value</Parameter> </ParameterGroup> </Parameters>

    Doing this shows us our data structure. Now we just need to build it before translating to xml:

    #!/usr/bin/perl use XML::Simple; use strict; use warnings; my $xml = { ParameterGroup => [ { ID => 'Group1', Parameter => { Key1 => {content => 'Some Value'}, Key2 => {content => 'Some Value'}, Key3 => {content => 'Some Value'}, }, }, { ID => 'Group1', Parameter => { Key1 => {content => 'Some Value'}, Key2 => {content => 'Some Value'}, Key3 => {content => 'Some Value'}, }, }, ]}; print XMLout($xml, RootName => 'Parameters', KeyAttr => 'key', );

    Outputs

    <Parameters> <ParameterGroup ID="Group1"> <Parameter key="Key1">Some Value</Parameter> <Parameter key="Key2">Some Value</Parameter> <Parameter key="Key3">Some Value</Parameter> </ParameterGroup> <ParameterGroup ID="Group1"> <Parameter key="Key1">Some Value</Parameter> <Parameter key="Key2">Some Value</Parameter> <Parameter key="Key3">Some Value</Parameter> </ParameterGroup> </Parameters>
      thanks wind.. this works.. but the user would have to specify the values of tags as an when he is scripting.. and therefore I cannot hard code the values inside the tag.. for this purpose..I have something like..
      $xml = { Parameter => [] }; push( @{ $xml->{Parameter} } , { key => $hash->{Key}, content => $hash->{Value} } );
      but this prints something like
      <Parameter key="ID1">Some Process a</Parameter> <Parameter key="ID2">Some Process b</Parameter> <Parameter key="ID3">Some Process c</Parameter>
      if I also want the ParameterGroup ID to be given by the user how can i do it..
        Then don't output the xml until the user has specified the complete data set, and translate it just like I showed.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-19 18:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found