GetSimple Support Forum
write XML line by line - Printable Version

+- GetSimple Support Forum (http://get-simple.info/forums)
+-- Forum: GetSimple (http://get-simple.info/forums/forumdisplay.php?fid=3)
+--- Forum: Developer Discussions (http://get-simple.info/forums/forumdisplay.php?fid=8)
+--- Thread: write XML line by line (/showthread.php?tid=7474)



write XML line by line - almendro - 2015-08-12

Hi everyone, this is my first post. Thanks for this great CMS and the community behind.

(english is not my mother language so, please, forget me for my poor spelling)

I use GIT for versioning my sites and the problem are the XMLs in one big line, so I decide break it tag by tag.

In admin/inc/caching_functions.php, at the end of file after saving the data:

Code:
$xml->asXML($filem);
    
    // PATCH XML line by line
    $tmpXMLfile = file_get_contents($filem);
    $tmpNTC = "###DONT_BREAK_CDADA###";
    
    // insert between the opening TAG and CDATA, and the closing too.
    $tmpXMLfile = str_replace("><![CDATA",">$tmpNTC<![CDATA",$tmpXMLfile);
    $tmpXMLfile = str_replace("]]></","]]>$tmpNTC</",$tmpXMLfile);
    
    // insert new line (linux) between ><
    $tmpXMLfile = str_replace("><",">\n<",$tmpXMLfile);
    
    // clean DONT_BREAK_CDATA and write the file again.
    $tmpXMLfile = str_replace("$tmpNTC","",$tmpXMLfile);
    file_put_contents($filem,$tmpXMLfile);



Now, every change made with de admin panel will write XMLs TAGs separated by lines and can be follow the diffs with GIT or another version control system.

I hope this will be useful to somebody.

Best.


RE: write XML line by line - Angryboy - 2015-08-12

Would you like to also post this as an issue on the GetSimple Github repository? Perhaps even fork the project and make a pull request for the change?


RE: write XML line by line - almendro - 2015-08-12

(2015-08-12, 03:45:05)Angryboy Wrote: Would you like to also post this as an issue on the GetSimple Github repository? Perhaps even fork the project and make a pull request for the change?

Thanks, I will do soon.


RE: write XML line by line - shawn_a - 2015-08-12

You could just use the aftersave hooks to post process it


RE: write XML line by line - Angryboy - 2015-08-12

Expanding on what shawn said (if almendro wants to write it as a plugin and publish it to extend):

PHP Code:
<?php
// plugins/prettify_pagecache.php
// Register the plugin
register_plugin(/*  registration details */);

// Add the page saving hook
add_action('pagecache-aftersave''prettify_pachecache');

// Define the prettifying function
function prettify_pachecache() {
 
 // get the xml file ($filem) and then run almendro's prettifying script


I still think almendro's suggestion is worth making an issue/pull request on (e.g. having a constant in gsconfig.php that enables/disables xml prettifying).


RE: write XML line by line - shawn_a - 2015-08-12

Nope I'll add a filter instead


RE: write XML line by line - shawn_a - 2015-08-13

even better
Added in to core
AND added a filter

https://github.com/GetSimpleCMS/GetSimpleCMS/issues/1089


RE: write XML line by line - Angryboy - 2015-08-13

Nicely done!


RE: write XML line by line - almendro - 2015-08-13

Wow, this is so fast. Thanks dudes! I'm glad to see this was happened.

Best!


RE: write XML line by line - almendro - 2015-08-13

(2015-08-13, 04:03:26)shawn_a Wrote: even better
Added in to core
AND added a filter

https://github.com/GetSimpleCMS/GetSimpleCMS/issues/1089

an honor to contribute into the CORE! thanks. Big Grin


RE: write XML line by line - shawn_a - 2015-08-13

Thanks for the suggestion, this would have actually saved me a ton of time last week, I had my text editor refreshing and reformatting constantly so i could read a file, was very annoying while i was testing output of something.

Always do a backup before testing a beta feature, hopefully it wont break anything but since it modifies all xml it could.
Needs testing with entities and utf-8, my tests seemed to be ok so far.