Editing XML using PHP for Project Manipulation

I had an idea about creating tutorial that will cover manipulating XML in PHP. The idea was to create some website that will use XML as a database.You can use it for password storage for your website admin and even your website configuration.

In this tutorial we'll try to edit some data in XML. Our XML looks like this.

<!--xml version="1.0" encoding="UTF-8"?--><br /> <fruits><br /> <item><br /> <name>Apple</name><br /> <price>10.25</price><br /> <count>15</count><br /> </item><br /> </fruits><br />
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 Elements
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 Elements Top


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 Element Top

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 Output Top

Now you can save XML by using

$xml->save()

or output it using

echo $xml->saveXML()

I hope you enjoyed it

Comments

  1. Damn good so simple and easy to learn that difficult thing.. i was trying to make it but wasn't able to do so thanx kuldeep once again you rock

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This manipulation is easy using this PHP. Its great and interesting news also. thanks for sharing

    ReplyDelete
  4. @Nelson yea its easy one but you know there are freshers as well :D

    ReplyDelete

Post a Comment

Popular posts from this blog

Getting Twitter Homeline using Prototype

Execute Simple MySql Queries in Joomla

What Is Bitcoin? 10 Facts You Should Know