Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

XML format help

by zdog (Priest)
on Aug 22, 2001 at 00:31 UTC ( #106762=perlquestion: print w/ replies, xml ) Need Help??
zdog has asked for the wisdom of the Perl Monks concerning the following question:

I have an XML file that looks something like this:
<opt> <A> <B> <C> <x>123</x> </C> </B> </A> </opt>
Then, using XML::Simple, I do XMLin() and XMLout() on it and it returns something looking like this:
<opt> <A name="B"> <C> <x>123</x> </C> </A> </opt>
This second way parses the same way with XMLin() as the first one, and then I add a field using a certain function and then XMLout() returns it yet again a different way:
<opt> <A name="B"> <B name="C"> <x>456</x> </B> <C x="123" /> </A> </opt>
This of course does not parse correctly with XMLin() causing my functions that operate on it to be useless.

Now the question: is there any module or method that I can use that will allow me to keep the initial XML format throughout the runnings of the program?

Zenon Zabinski | zdog |

Comment on XML format help
Select or Download Code
Replies are listed 'Best First'.
I finally get to help. *Smiles*
by Rhose (Priest) on Aug 22, 2001 at 01:26 UTC
    What you are experiencing can be corrected by setting forcearray => 1 with XMLin(). I threw together a quick example which will demonstrate the differences. (Your sample XML file is in.xml.)

    One other thing I have found to be helpful when working with XML::Simple is Data::Dumper.

    #-- Sample code use strict; use XML::Simple; my $mXML; print "Without forcearray...\n"; $mXML = XMLin('./in.xml'); print XMLout($mXML); print "\n\n"; print "With forcearray...\n"; $mXML = XMLin('./in.xml',forcearray => 1); print XMLout($mXML);
Re: XML format help
by princepawn (Parson) on Aug 22, 2001 at 00:47 UTC
    I strongly recommend XML::Twig. This is your dream XML module. Search for XML::Twig here on the site. I just posted 2 tutorials (and need to post my third)... thanks for reminding me.

    And to answer your specific question. yes, you can traverse the entire tree without modification if you please. And you can also generate new trees as well.

Re: XML format help
by IraTarball (Monk) on Aug 22, 2001 at 00:56 UTC
    You should read up on the options for XML::Simple I think what's tripping you you up is the keyattr option. This defaults to name but you probably want it set to the empty string. You might also check out Data::Dumper to view the data structures that XML::Simple creates.

    I've been using XML::Simple for reading in and writing out config files without any unwanted changes to the xml file so I'm pretty sure it will do what you want.

    Hope this helps,

    "So... What do all these little arrows mean?"

Re: XML format help
by alowishus (Initiate) on Aug 22, 2001 at 00:43 UTC
    I've found that using these attributes to xml out should help to keep the xml in the same format. check perldoc XML::Simple for more options that might help you. keyattr=>[],noattr=>1

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://106762]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2015-11-26 00:37 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (694 votes), past polls