What do you think I should do?

I think you shouldn't use XML::Simple! There are lots of issues with that module, especially with writing XML. There are several much better modules for XML handling, such as XML::Rules (which can often replace XML::Simple for reading XML) or XML::Twig, but for writing XML, personally I like to use XML::LibXML because it gives me the most control. (Update: Note the following makes some assumptions about the input data format, such as that you don't have any arrayrefs, as well as the output format, because the sample data you've provided is pretty short, and you haven't explained what rules should be used for conversion. But I hope it's a starting point.)

use warnings; use strict; use XML::LibXML; my $hash = { 'root' => { 'item1' => { 'item1a' => 'n1', 'item1b'=>'jh' } } }; my $dom = XML::LibXML::Document->createDocument(); # recursively convert hashes to elements my $frag = $dom->createDocumentFragment(); my $hash2el; $hash2el = sub { my ($n,$h) = @_; for my $key (sort keys %$h) { my $el = $dom->createElement($key); if (ref $h->{$key} eq 'HASH') { $hash2el->($el, $h->{$key}) } else { $el->appendText( ''.$h->{$key} ) } $n->appendChild($el); } }; $hash2el->($frag, $hash); # either a single root node, or multiple nodes in a new root my @roots = $frag->nonBlankChildNodes(); if (@roots==1) { $dom->setDocumentElement( $roots[0] ); } elsif (@roots>1) { my $root = $dom->createElement('root'); $root->appendChild($frag); $dom->setDocumentElement( $root ); } print $dom->toString(1); __END__ <?xml version="1.0"?> <root> <item1> <item1a>n1</item1a> <item1b>jh</item1b> </item1> </root>

In reply to Re: XMLOut in XML::Simple returning unwanted xml format by haukex
in thread XMLOut in XML::Simple returning unwanted xml format by perl_help27

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.