Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: xml::libxml open, add and save not formatting properly

by ikegami (Patriarch)
on Mar 23, 2010 at 22:53 UTC ( [id://830413]=note: print w/replies, xml ) Need Help??


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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://830413]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-19 21:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found