in reply to xml::libxml open, add and save not formatting properly

Don't you want ->toString(2)?

chmod 0664, $outfile;

that's not the right variable name. Aren't you using use strict; use warnings;?

autoflush XMLfile 1;

Useless, since closing a file handle flushes it.

binmode(XMLfile,":utf8");

That's a bug. "on document nodes [toString] returns the XML as a byte string in the original encoding of the document". You're double encoding. You want

# Switch to UTF-8 if it's not already. $config->setEncoding('UTF-8'); open(my $config_fh, ">", $configuri) or die $!; binmode($config_fh); print $config_fh $config->toString(2); close($config_fh); chmod 0664, $configuri;
or better yet:
# Switch to UTF-8 if it's not already. $config->setEncoding('UTF-8'); $config->toFile($configuri, 2); chmod 0664, $configuri;

Replies are listed 'Best First'.
Re^2: xml::libxml open, add and save not formatting properly
by itsscott (Sexton) on Mar 23, 2010 at 23:12 UTC
    Thanks for the quick response and information, I did make all the changes you recommended and it did not make a difference (please forgive any 'code' errors on the example, I had to extract it from our code and re-create it for the question due to a non-disclosure agreement.
    As you can see, the first 'site' is nice, and the one I just added in my test is not (in fact the </sites> has also lost it's linefeed in the process.
    <?xml version="1.0" encoding="UTF-8"?> <config> <sites> <site> <sitename><![CDATA[www.example.com]]></sitename> <active><![CDATA[1]]></active> <rooturl><![CDATA[http://www.example.com.com/]]></rooturl> <name><![CDATA[Example]]></name> </site> <site><sitename>Test entry</sitename><name></name><rooturl><![CDATA[ +http://www.test.com.com/]]></rooturl><reportname><![CDATA[test report + name]]></reportname></site></sites> </config>
    Again, this is just a small section of many entries in this file.

      The catch is that what you're asking to do involves changing the logical structure of the XML document by adding significant spaces, and XML::LibXML sees toString as a serialization function.

      and it did not make a difference

      I just tried it. It makes a huge difference. Not for the good, though. While it pretties up the part that isn't prettied up, it pretties up the part that's already been prettied up too.

      use strict; use warnings; use XML::LibXML; print XML::LibXML->new->parse_fh(*DATA)->toString(2); __DATA__ <?xml version="1.0" encoding="UTF-8"?> <config> <sites> <site> <sitename><![CDATA[www.example.com]]></sitename> <active><![CDATA[1]]></active> <rooturl><![CDATA[http://www.example.com.com/]]></rooturl> <name><![CDATA[Example]]></name> </site> <site><sitename>Test entry</sitename><name></name><rooturl><![CDATA[ +http://www.test.com.com/]]></rooturl><reportname><![CDATA[test report + name]]></reportname></site></sites> </config>
      ?xml version="1.0" encoding="UTF-8"?> <config> <sites> <site> <sitename> <![CDATA[www.example.com]]> </sitename> <active> <![CDATA[1]]> </active> <rooturl> <![CDATA[http://www.example.com.com/]]> </rooturl> <name> <![CDATA[Example]]> </name> </site> <site> <sitename> Test entry </sitename> <name/> <rooturl> <![CDATA[http://www.test.com.com/]]> </rooturl> <reportname> <![CDATA[test report name]]> </reportname> </site> </sites> </config>
        Yes it is a little odd, I am using the 1 option as opposed to the 2 option as I don't like all the carriage returns :-) I've toyed and toyed with this for a whole day last week and it's just odd, that the document I created in the first place was with the same exact methods, steps etc as the 'revised' document. All this effort so I can make a new entry in this xml file and the lead on the project likes it a certain way.

        I do thank you all for your input, even if I can't solve this the way I want, I have learned new things!