View Full Version : XML with PHP
Moshambi
02-10-2009, 03:30 AM
Hello everyone,
I am wondering if there is some way to open an XML document, make changes to it and then save the changes. I specifically want to know if there is a way that I can add new elements to the document. I have searched the net for this and have not been able to come up with anything so now I'm asking the pros. Thanks in advance!
Mosh
I don't know if there is an xml function you can use... But you can try making your own with:
file_put_contents (http://us3.php.net/manual/en/function.file-put-contents.php) & file_get_contents (http://us3.php.net/manual/en/function.file-get-contents.php)
fgetcsv (http://us3.php.net/manual/en/function.fgetcsv.php)
fwrite (http://us3.php.net/manual/en/function.fwrite.php)
Maybe some of those.
techietim
02-10-2009, 11:07 AM
The SimpleXML (http://php.net/simplexml) object is the quickest way to go about this. Post again if you need help with it.
JasonDFR
02-10-2009, 12:29 PM
Hello everyone,
I'm asking the pros. Thanks in advance!
Mosh
I'm not a pro, but
http://www.php.net/manual/en/book.simplexml.php
Is fairly straight forward. I played with it for a couple hours and then felt fairly comfortable.
Didn't see Tim's post. Sry.
Moshambi
02-10-2009, 06:46 PM
Ok ya I had been looking at the simpleXMLElement stuff before I posted this but I still can't figure out a way to add an element to the xml document then save it. For example:
XML Doc before I run Code:
<?xml version="1.0"?>
<body>
<element>text</element>
</body>
XML Doc after Code:
<?xml version="1.0"?>
<body>
<element>text</element>
<element>text</element>
</body>
Does this make any sense? I tried using the addChild() function but I don't know how to get it to save itself. I tried $variable->saveXML();
It will output it but it won't actually save it to the document?
Thanks for the help,
Mosh
techietim
02-10-2009, 08:16 PM
<?xml version="1.0"?>
<body>
<element>text</element>
</body>
$simpleXML = new SimpleXMLElement('path/to/xmlfile.xml', 0, true);
$simpleXML->addChild('element2', 'innerText');
$simpleXML->asXML('path/to/xmlfile.xml');
<?xml version="1.0"?>
<body>
<element>text</element>
<element2>innerText</element2>
</body>
Moshambi
02-11-2009, 08:15 AM
AWESOME! That was exactly what I was looking for. Thank you very much!
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.