Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Problem saving XML

by rpike (Scribe)
on May 11, 2012 at 16:02 UTC ( #970050=perlquestion: print w/replies, xml ) Need Help??
rpike has asked for the wisdom of the Perl Monks concerning the following question:

I read in and parse the below XML fine, it populates a hash with the values I want. In one area I want to increment the nextSessionID from the XML read in and then write the XML document back. When I run the below I get the XML document overwritten with an element with all other named tags being attributes of the main tag. Starting XML
<?xml version="1.0" encoding="UTF-8"?> <mainTag> <items> <nextSessionID>1000</nextSessionID> </items> </mainTag>
After write I get
<?xml version='1.0'?> <items nextSessionID="1001" />
The segment of the program that deals with the XML document being read in and written out.
my $xmlSimp = XML::Simple->new(); my $xmlCont = $xmlSimp->XMLin($xmlPath); my $varIn = ""; our %xmlHash; foreach my $keyI (keys (%{$xmlCont->{"items"}})) { $varIn = $xmlCont->{"items"}->{$keyI}; $xmlHash{$keyI} = $varIn; if ($keyI eq "nextSessionID") { $xmlCont->{"items"}->{$keyI} = ++$varIn; } } my $tmpOut = $xmlSimp->XMLout($xmlCont, KeepRoot => 1, OutputFile => $xmlPath, XMLDecl => "<?xml version='1.0'?>" );

Replies are listed 'Best First'.
Re: Problem saving XML
by Corion (Pope) on May 11, 2012 at 16:07 UTC

    You likely want to read up on the SuppressEmpty option, and maybe restructure your data structure so that XML::Simple recognizes that nextSessionID should be a tag, not an attribute. Maybe KeyAttr is the relevant thing to use there.

      Thanks for the info. Any chance you could put an example of what you're referring to? I'm trying some stuff but nothing seems to be working. Also, why would the mainTag go missing altogether? I haven't touched this in a long time and even then I haven't done much with it. Any further help you can give would be much appreciated. Thank-you.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://970050]
Approved by Corion
and the rats come out to play...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2018-06-20 02:37 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.