I had an idea about creating tutorial that will cover manipulating XML in PHP. The idea was to create some software that will use XML as a database. But then I realized that it will take me to much writing (about 100 pages) so I may create some video tutorials later. But since I’ve got a lot of requests where readers asked from me to create some tutorial about editing XML so I decided to create simple tutorial.
XML TemplateTop
In this tutorial we’ll try to edit some data in XML. Our XML looks like this.
< ?xml version="1.0" encoding="UTF-8"?>
<fruits>
<item>
<name>Apple</name>
<price>10.25</price>
<count>15</count>
</item>
</fruits>
We’ll load item element and change it’s content. Then we’ll create new item element and append it to fruits after first item. So let’s start.
Loading Document And ElementsTop
First we have to create new DOMDocument class instance. We’ll set XML version and encoding. Next we’ll set formatOutput to true so we can have nice output. We’ll also set preserveWhiteSpace to false because there is some bug in PHP that will not save nicely newly created elements. Now we just have to load our XML file.
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('fruits.xml');
Next we’ll load item element.
$element = $xml->getElementsByTagName('item')->item(0);
Now we’ll load each child element of item so we can manipulate with them. If you don’t know how to load them, check my previous tutorial about XML here.
$name = $element->getElementsByTagName('name')->item(0);
$price = $element->getElementsByTagName('price')->item(0);
$count = $element->getElementsByTagName('count')->item(0);
Changing Values Of ElementsTop
Now we’ll change some values. We’ll change name and price. Element count we’ll change by replacing it with a new element. We also could change it like name and price, but this is just to show you an other way.
$name->nodeValue = 'Orange'; $price->nodeValue = 11.33;
Next we’ll create our new count element. We do that by calling createElement and pass as arguments elements name and it’s value.
$newCount = $xml->createElement('count', 10);
Now we have to replace changed elements with old elements in XML. For name and price we can use same object (element) as both arguments. When replacing count we have to pass new element and old element so PHP knows which element to replace.
$element->replaceChild($name, $name); $element->replaceChild($price, $price); $element->replaceChild($newCount, $count);
Creating New ElementTop
Let’s say we want to add new element. We can do it by creating new item element and them appending new count, price, and count elements to item like in next few lines. We just create new elements and append them.
$newItem = $xml->createElement('item');
$newItem->appendChild($xml->createElement('name', 'Strawberry'));
$newItem->appendChild($xml->createElement('price', 9.59));
$newItem->appendChild($xml->createElement('count', 31));
Now we must not forget to add it to XML. We’ve created new element but did not add it. We do that by selecting fruits element and appending new item element that we created previously.
$xml->getElementsByTagName('fruits')->item(0)->appendChild($newItem);
Save And OutputTop
Now you can save XML by using
$xml->save($location)
or output it using
echo $xml->saveXML()
.
This will conclude our tutorial. You can check demo here or download source code here. Thanks for reading and I hope it was helpful.
Thank you
Thanks for this, this was totally digestible for a PHP (and mostly coding) novice. I’m excited to look back on your original XML tutorial now. =) Cheers!
Select roots children and go through just first two elements (use counter or something like that)
Hi, Thanks for this tutorial. What if I want to replace and attribute in a child rather than an element? eg:
<root>
<set name=’a’ value=’alpha’/>
<set name=’b’ value=’beta’/>
<set name=’c’ value=’gamma’/>
</root>
In this I want to change the first two child’s attributes leaving the third the same.
I like your tutorial very much. It solves a problem that i have partly.
My next biggest challenge can be found in the following forum.
http://forum.codecall.net/php-development/43028-php-xml-challenge.html
Regards.
It is. But WP puts that space in there
First code block:
< ?xml
should be:
<?xml
It yields error 500
Otherwise, nice and easy tutorial.
Thanks!
Great tutorial! Although I still cannot figure out how to finish my asg..
How do you mean does not change? In my tutorial I just output changed XML. I didn’t saved it in file so that change is permanent.
Thank u
this will done but the xml file not be changed pls help me
Great tutorial! I was a bit confused with the DOM/XML interaction, but thanks to your clear explanations I’m making things work, finally
Thank you!
hi! thank you for your for your helpfull tutorial. i’d liked it so much. but i have one task and can’t solve it. how can i change or replace CDATA content in my xml file? can you help me please? thanks
You should use save method or save XML string you get from saveXML using function like file_put_contents.