in reply to Re^2: xml::libxml open, add and save not formatting properly
in thread xml::libxml open, add and save not formatting properly

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>

Replies are listed 'Best First'.
Re^4: xml::libxml open, add and save not formatting properly
by itsscott (Sexton) on Mar 23, 2010 at 23:57 UTC
    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!
      xmllint is a tool that uses libxml and comes with it. It's --format option does what you want. All the work appears to be done by a library function htmlSaveFileFormat. You could patch XML::LibXML to expose that function. You're not the first whose wanted a pretty print function from XML::LibXML.

      Update: The simplest solution would be to add an optional format argument to $doc->toStringHTML and swap in htmlDocDumpMemoryFormat for htmlDocDumpMemory in that function.

      Update: Never mind, htmlDocDumpMemory simply calls htmlDocDumpMemoryFormat with the format option set, and it doesn't help.