How can I edit the value's in a xml file using simpleXML ?
I know how to create the file, but not how to edit the value in an existing file ?
The way to change the value of an attribute, is to change its text value. This can be done using the setAttribute() method or setting the nodeValue property of the attribute node.
SimpleXML is an extension that allows us to easily manipulate and get XML data. SimpleXML provides an easy way of getting an element's name, attributes and textual content if you know the XML document's structure or layout.
Example #2 Create a SimpleXMLElement object from a URL$sxe = new SimpleXMLElement('http://example.org/document.xml', NULL, TRUE); echo $sxe->asXML();
I am working like this (it's quite the same but it could help): The file test.xml could be any extension as long as it's a plain xml text.
test.xml:
<?xml version="1.0" encoding="utf-8"?>
<sitedata>
<Texts>
<ANode SomeAttr="Green" OtherAttr="Small"/>This is the text I'm changing.</ANode>
</Texts>
</sitedata>
And the PHP code:
$xml=simplexml_load_file("test.xml") or die("Error: Cannot create object");
$SomeVar="<b>Text. This supports html code.</b><br/>I also work with variables, like GET or POST.";
$xml->Texts[0]->{'ANode'}=$SomeVar;
$xml->asXml('test.xml');
Results test.xml:
<?xml version="1.0" encoding="utf-8"?>
<sitedata>
<Texts>
<ANode SomeAttr="Green" OtherAttr="Small"/><b>Text. This supports html code.</b><br/>I also work with variables, like GET or POST.</ANode>
</Texts>
</sitedata>
Hope it helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With