djburner Posted December 24, 2012 Share Posted December 24, 2012 (edited) Hi there, I was working in a XML file that I need to be updated by PHP, my problem is that using this snippet: <?php $xml = simplexml_load_file("myXml.xml"); $sxe = new SimpleXMLElement($xml->asXML()); $user = $sxe->addChild("user"); $user->addChild("name", "Ed Doe"); $user->addChild("age", "34"); $sxe->asXML("myXml.xml"); ?> After the php has run the XML has actuallly the updated information but here comes the catch: If I open the file in the browser like http://...myXml.xml the estructure does look great, indent spaces, breaks and all like in this form: <user> <name>Ed Doe</name> <age>34</age> </user> But when I try to see it in dreamweaver or another editor, the XML updated information does appear like a single line without any breaks like: <user><name>Ed Doe</name><age>34</age></user> Which is really going to get really messy when more data is updated since every entry as new user just extends the long single line. How could I add this indent spaces using PHP? Is there a way like: $myItem ->addChild("age", "34", <br/>); Hope anyone could give me a hand on this one. Greetings and thanks in advance. Edited December 24, 2012 by djburner Quote Link to comment https://forums.phpfreaks.com/topic/272340-indent-addchild-element-in-xml/ Share on other sites More sharing options...
Cainj Posted December 24, 2012 Share Posted December 24, 2012 wow, I had this prob for eons!! for some time I kept working on my project, but eventually this "single line" issue bugged me. his is a function I made to correct it. :-) enjoy @filename must be full path (or relative), and $xml is the $xml data modify as needed function saveXMLFile($filename, $xml) { $dom = dom_import_simplexml($xml)->ownerDocument; $dom->formatOutput = true; $fh = fopen($filename,'w'); fwrite($fh,$dom->saveXML()); fclose($fh); } YOUR CODE can be changed as such: $xml = simplexml_load_file("myXml.xml"); $sxe = new SimpleXMLElement($xml->asXML()); $user = $sxe->addChild("user"); $user->addChild("name", "Ed Doe"); $user->addChild("age", "34"); // $sxe->asXML("myXml.xml"); dont need this line function saveXMLFile($xml, $sxe); Quote Link to comment https://forums.phpfreaks.com/topic/272340-indent-addchild-element-in-xml/#findComment-1401192 Share on other sites More sharing options...
djburner Posted January 4, 2013 Author Share Posted January 4, 2013 @Cainj Hi man, thanks for your snippet, it does look preety good, but I have some problems with it. My code looks like this: <?php $xml = simplexml_load_file("menu.xml"); $sxe = new SimpleXMLElement($xml->asXML()); $disenoItem = $sxe->addChild("disenoItem"); $disenoItem->addChild("name", "Ed Doe"); $disenoItem->addChild("age", "30"); saveXMLFile($xml, $sxe); function saveXMLFile($filename, $xml) { $dom = dom_import_simplexml($xml)->ownerDocument; $dom->formatOutput = true; $fh = fopen($filename,'w'); fwrite($fh,$dom->saveXML()); fclose($fh); } ?> What am I doing wrong, actually the FTP trows this error: TypeError: items[x][0] is undefined[] Thanks if you can check how I used your snippet in order to spot out the problem. I only delete the world "function" from your snippet right here: // $sxe->asXML("myXml.xml"); dont need this line function saveXMLFile($xml, $sxe); //I changed the above sentence into this: saveXMLFile($xml, $sxe); //Cause I think It was an error in your snippet, am I right? Greetings and thanks in advance for any futher help on this issue. Quote Link to comment https://forums.phpfreaks.com/topic/272340-indent-addchild-element-in-xml/#findComment-1403269 Share on other sites More sharing options...
salathe Posted January 5, 2013 Share Posted January 5, 2013 djburner, look at the arguments you are passing in to the function and how the function uses them. In particular, the $filename argument, which should be a string containing the path to the file that should be written. Also, there is no need to use fopen() and friends to write the file. Instead use $dom->save($filename). Quote Link to comment https://forums.phpfreaks.com/topic/272340-indent-addchild-element-in-xml/#findComment-1403418 Share on other sites More sharing options...
djburner Posted January 8, 2013 Author Share Posted January 8, 2013 Hi @Badger, I've change the snippet as I think you told me to do, but now is not doing anything. I know how to work with Javascript pretty well, but in PHP I'm just a total rookie, sorry if I do something really off the line. <?php $xml = simplexml_load_file("menu.xml"); $sxe = new SimpleXMLElement($xml->asXML()); $disenoItem = $sxe->addChild("disenoItem"); $disenoItem->addChild("name", "Ed Doe"); $disenoItem->addChild("age", "30"); saveXMLFile($xml, $sxe); function saveXMLFile($filename, $xml) { $dom = dom_import_simplexml($xml)->ownerDocument; $dom->formatOutput = true; $dom->save(menu.xml); // I used both (menu.xml) as well ($filename) but neither seems to work } ?> Hope you can see what is wrong in this approach. Thank you fro your time and help guys. Greetings. Quote Link to comment https://forums.phpfreaks.com/topic/272340-indent-addchild-element-in-xml/#findComment-1404082 Share on other sites More sharing options...
salathe Posted January 8, 2013 Share Posted January 8, 2013 When calling the function: saveXMLFile('menu.xml', $sxe); Inside the function: $dom->save($filename); Quote Link to comment https://forums.phpfreaks.com/topic/272340-indent-addchild-element-in-xml/#findComment-1404125 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.