Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Need change in dynamic XML Structure using XML::Simple

by Perllace (Acolyte)
on Apr 29, 2011 at 10:53 UTC ( [id://901957]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I need to dynamically generate an XML structure. With this code..
use strict; use warnings; use XML::Simple; BEGIN { @__PACKAGE__::XML::ISA = qw/ XML::Simple /; my %order = ( TestResult => [ qw/xmlns xmlns:xsi xsi:schemaLocation Result Comments Para +meters Files Images /], ); *__PACKAGE__::XML::sorted_keys = sub { my ( $self, $name, $h ) = @_; return @{ $order{$name} } if $order{$name}; sort keys %$h; }; } sub new { my ($class) = @_; my $self = { _xml => { "xmlns" => "Someinfo", "xmlns:xsi" => "Someinfo", "xsi:schemaLocation" => "Someinfo", }, }; bless $self => $class; } sub comments { my ( $self, $comments ) = @_; $self->{_xml}{Comments} = { content => $comments }; } sub result { my ( $self, $result ) = @_; $self->{_xml}{Result} = { content => $result }; } sub image { my ( $self, $image, $desc ) = @_; my $imageid = "Image" . $image; $self->{_xml}{Images}{$imageid} = { content => $desc }; } sub file { my ( $self, $file, $desc ) = @_; my $fileid = "File" . $file; $self->{_xml}{Files}{$fileid} = { content => $desc }; } sub parameter { my ( $self, $group, $key, $value ) = @_; my $groupid = "Group".$group; my $keyid = "Key".$key; $self->{_xml}{Parameters}{Parameter}{$groupid}{Parameter}{$keyid} += { content => $value }; } sub as_xml { my ($self) = @_; my $file_name = "output.xml"; my $serialize = __PACKAGE__::XML->new; my @arrayout= $serialize->XMLout( $self->{_xml}, GroupTags => { Files => "File", Images => "Image", Parameters => "ParameterGroup", ParameterGroup => "Parameter" }, KeyAttr => { ParameterGroup => "+ID", Parameter => "+key", File => "+key", Image => "+key", }, RootName => "TestResult", XMLDecl => 1, ); foreach ( @arrayout ) { open FH, ">>$file_name" or die "can't open '$file_name': $!"; print FH $_; close FH; }
Using this below script,
my $tr = Reporting->new; $tr->parameter(1, 1, "Some Process A Happened"); $tr->parameter(1, 2, "Some Process B Happened"); $tr->parameter(2, 1, "Some Process A Happened"); $tr->parameter(3, 1, "Some Process Happened"); $tr->image(1, "Some info"); $tr->parameter(6, 1, "Some Process Z Happened"); $tr->file(10, "Some File"); $tr->result("Pass"); $tr->comments("Executed Successfully"); print $tr->as_xml;
I was able to get this something like
<?xml version='1.0' standalone='yes'?> <TestResult xmlns="some info" xmlns:xsi="some info" xsi:schemaLocation +="some info"> <Result>Pass</Result> <Comments>Executed Successfully</Comments> <Parameters> <ParameterGroup ID="Parameter"> <Parameter key="Group1"> <Parameter key="1">Some Process A Happened</Parameter> <Parameter key="2">Some Process B Happened</Parameter> </Parameter> <Parameter key="Group2"> <Parameter key="1">Some Process A Happened</Parameter> </Parameter> <Parameter key="Group3"> <Parameter key="1">Some Process Happened</Parameter> </Parameter> <Parameter key="Group6"> <Parameter key="1">Some Process Z Happened</Parameter> </Parameter> </ParameterGroup> </Parameters> <Files> <File key="File10">Some File</File> </Files> <Images> <Image key="Image1">Some info</Image> </Images> </TestResult>
But I need the parameters part of the XML to look like this,
<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>
Where is it going wrong? Thanks..

Replies are listed 'Best First'.
Re: Need change in dynamic XML Structure using XML::Simple
by Anonymous Monk on Apr 29, 2011 at 13:47 UTC

    Similar question: 'when I run Linux I don't see the Windows start icon...' or 'I want to travel to the moon...with a golf cart...'

    Your question prompts an obvious one -- how did you get this far with your code and expected output without understanding what you were doing?

      So you mean to say..I can't do this..
        So you mean to say..I can't do this..

        So you mean to say you did not write this code?

        The other Anonymous monk
Re: Need change in dynamic XML Structure using XML::Simple
by wind (Priest) on Apr 30, 2011 at 00:14 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-03-19 11:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found