Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

xml using XML::Simple reording of XML-Content

by tobias_hofer (Friar)
on Jun 17, 2013 at 11:45 UTC ( [id://1039344]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks

I got this following problem:
I got a hash which I want to get into xml. My problem is that the content (keys) of the hash are ordered alphabetically. Thus the content comes into a different order to file. This is not a problem to xml itself but a problem for the tool-chain following my perl script, which expects things to be in a specific order

My hash is generated and looks in the end like this
my $TGROUP = { COLS => 4, COLSPEC => [... Array of hashes...], TBODY => HASH(0x8b...), THEAD => HASH(0x8bc...) }
and it comes it this order to xml
<TRGROUP... <COLS ... <COLSPEC .. <TBODY .... <THEAD ... >
but the tool requires it in a different order:
<TRGROUP... <COLS ... <COLSPEC .. <THEAD .... <TBODY ... >
Therefore it would be simplest to reorder the sequence of keys in the hash.. i.e. like that:
my $TGROUP = { COLS => 4, COLSPEC => [... Array of hashes...], THEAD => HASH(0x8b...), TBODY => HASH(0x8bc...) }
Is this somehow possible to do? I tried to create the hash once more in a different order but this does not work.

Any help is highly welcome!

best regards!
Tobias

Replies are listed 'Best First'.
Re: xml using XML::Simple reording of XML-Content
by hdb (Monsignor) on Jun 17, 2013 at 11:53 UTC

    In this post there was good advice: Re^2: XML::Simple how do i keep the same xml format (Values and Attributs)?

    UPDATE: Only way I see is to create 4 XML files for the second level in your tree according to the desired order, and then manually create the outer structure around them. For example:

    use strict; use warnings; use XML::Simple; my $TGROUP = { COLS => 4, COLSPEC => [ 1, 2, 3, 4 ], THEAD => { head => "head" }, TBODY => { body => "body" }, }; print "<TABLE>\n"; for ( qw( COLS COLSPEC THEAD TBODY ) ) { # in your desired order print XMLout( {$_ => $TGROUP->{$_}}, RootName => $_, KeepRoot => +1, NoATTR => 1 ); } print "</TABLE>\n";
Re: xml using XML::Simple reording of XML-Content
by Anonymous Monk on Jun 17, 2013 at 11:53 UTC
Re: xml using XML::Simple reording of XML-Content
by poj (Abbot) on Jun 17, 2013 at 15:27 UTC
    Any help is highly welcome!

    I'm no expert in this subject but here's a XSLT transform from the original XML into one with the correct order

    #!perl use strict; use XML::LibXSLT; use XML::LibXML; =head1 test.xsl <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Tr +ansform"> <xsl:output indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="TRGROUP"> <xsl:copy> <xsl:apply-templates select="COLS"/> <xsl:apply-templates select="COLSPEC"/> <xsl:apply-templates select="THEAD"/> <xsl:apply-templates select="TBODY"/> </xsl:copy> </xsl:template> </xsl:stylesheet> =cut my $parser = XML::LibXML->new(); my $xslt = XML::LibXSLT->new(); my $stylesheet = $xslt->parse_stylesheet_file('test.xsl'); my $xml; { local $/=''; $xml = <DATA>; } my $source_doc = $parser->parse_string( $xml ); my $result = $stylesheet->transform( $source_doc ); print $stylesheet->output_string( $result ); __DATA__ <TABLE> <TRGROUP> <COLS>AA</COLS> <TBODY>A</TBODY> <COLSPEC>XX</COLSPEC> <THEAD>1</THEAD> </TRGROUP> <TRGROUP> <COLSPEC>XX</COLSPEC> <TBODY>B</TBODY> <THEAD>2</THEAD> <COLS>BB</COLS> </TRGROUP> <TRGROUP> <TBODY>C</TBODY> <COLS>CC</COLS> <COLSPEC>ZZ</COLSPEC> <THEAD>3</THEAD> </TRGROUP> </TABLE>
    poj
Re: xml using XML::Simple reording of XML-Content
by tobias_hofer (Friar) on Jun 17, 2013 at 13:20 UTC
    Thanks a lot!
    I will switch to XMLLib!!

    Best regards!

    Tobias

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found