Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

XML::Simple Output

by batkins (Chaplain)
on Nov 19, 2003 at 00:24 UTC ( #308198=perlquestion: print w/replies, xml ) Need Help??

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

I'm using XML::Simple to read in an XML file, make a change to it, and then output the resulting XML. However, the output isn't formatted quite the way I want it. This is my code:
use XML::Simple; $xml = XMLin("pre.xml"); $xml->{core}->{test} = 'testeroo'; $res = XMLout($xml); print $res;
This is pre.xml:
<config> <core> <variable>ere</variable> </core> </config>
And this is the output:
<opt> <core test="testeroo" variable="ere" /> </opt>
Is there any way to make the result look like:
<opt> <core> <test>testeroo</test> <variable>ere</variable> </core> </opt>
If not, is there some other module that can make this happen?

Are you sure it was a book? Are you sure it wasn't.....nothing?

Replies are listed 'Best First'.
Re: XML::Simple Output
by Chmrr (Vicar) on Nov 19, 2003 at 00:32 UTC

    Check out the noattr option to XMLout:

    % perl -w use XML::Simple; $xml = XMLin("pre.xml"); $xml->{core}->{test} = 'testeroo'; print XMLout($xml, noattr => 1); ^D <opt> <core> <test>testeroo</test> <variable>ere</variable> </core> </opt> <

    Update: You may also be interested in the rootname => 'config' option.

    Networking -- only one letter away from not working
Re: XML::Simple Output
by Cody Pendant (Prior) on Nov 19, 2003 at 05:40 UTC
    I'd say this is rapidly becoming a Frequently Asked Question, and I say this in all humility because I've asked it myself. I've definitely seen it a couple of times lately.

    Is there a place for this in Q & A?

    Why does this module require two attributes to be set in order to make output have the same structure as input? I'm not complaining, I genuinely want to know. It's not terribly simple in that respect at least.



    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
      Why does this module require two attributes to be set in order to make output have the same structure as input?

      XML::Simple is optimised for reading XML. Some of the things it does to make your life easier when it reads XML make things more difficult if you try and write XML back out again. But if you're anything like me then you read XML far more often than you write it. The two operations have very little in common so one tool is unlikely to be well suited to both.

      I'd be the first to admit that the XML::Simple API is a mess. The primary reason is that it wasn't planned as a general purpose XML manipulation tool. It was designed for reading XML config files. The code to write XML back out again was added on later. If I had set out to write a tool that could round-trip XML perfectly every time then I would have ended up designing some sort of DOM-type thing and at some point I would have seen the error of my ways and given up.

      I'd say this is rapidly becoming a Frequently Asked Question

      Perhaps, but I'd wager that most people who are asking have not read the XML::Simple documentation sitting on their own hard drive, or the XML::Simple FAQ sitting next to it. The original poster for example had a choice of 22 options and apparently didn't even try one! The ForceArray option which would be one solution to the original question is even singled out in the documentation as one of the two most important options you need to read about before you use the module. You can lead a horse to water ...

        Hi, thanks for that. I think part of the problem is that baffled, frustrated people who are just getting started with XML see something called XML::Simple and think it's the answer to all their prayers. I know I did.

        Maybe I could suggest putting this one particular issue in the POD?

        And you know what? I just clicked on the link to the XML::Simple::FAQ on search.cpan.org and it's a 404...



        ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
      Is there a place for this in Q & A?
      XML::Simple already has a FAQ (in addition to complete documentation), so I'm not so sure (the Q&A section is not exactly a module specific FAQ).
      Why does this module require two attributes to be set in order to make output have the same structure as input? I'm not complaining, I genuinely want to know. It's not terribly simple in that respect at least.
      That's how the module works (ask the author).

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2022-12-10 04:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?