Consider :

use XML::Simple; my $struct = XMLin( \*DATA ); print XMLout($struct, noattr => 1 ); __DATA__ <myconfig_data name="aaa" > <level1 my_att="somestring"> <value>hello1</value> </level1> <level2> <value other_att="bbb">hello2</value> </level2> <level3> <value>hello3</value> </level3> </myconfig_data>

Gives result ( almost as desired ) :

<opt> <name>aaa</name> <level1> <my_att>somestring</my_att> <value>hello1</value> </level1> <level2> <name>value</name> <content>hello2</content> <other_att>bbb</other_att> </level2> <level3> <value>hello3</value> </level3> </opt>

First question : Why do I have element 'opt' not <myconfig_data> at the docroot level ?

My main question : Can this be done within XML::Twig ? My reason being, for some twig handlers, I wish to convert all attributes to tags...

The nearest I can get is as follows :

use Data::Dumper; use strict; use XML::Twig; my $xml_twig = XML::Twig->new( pretty_print => 'indented', NoLWP => 1, discard_spaces => 1, ); $xml_twig->parse ( \*DATA ) || die "\nError parsing data $@\n"; my $struct=$xml_twig->simplify( noattr => 1 ); print Dumper($struct); __DATA__ <myconfig_data name="aaa" > <level1 my_att="somestring"> <value>hello1</value> </level1> <level2> <value other_att="bbb">hello2</value> </level2> <level3> <value>hello3</value> </level3> </myconfig_data>
This creates the following structure :
$VAR1 = { 'level1' => { 'value' => 'hello1' }, 'level2' => { 'value' => 'hello2' }, 'level3' => { 'value' => 'hello3' } };

Clearly this isnt correct. Variuos combinations of noattr =>0, / keeproot / keepattr havent helped.

Also, if the data structure was correct, how do I then use this to output my XML ?

Thanks.


In reply to XML::Simple $elt->noattr is there an equivalent using XML::Twig by jxh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.