PHP Introduction | Using XML Data | Manipulating Nodes

There may be cases where you need to do more than simply extract and output XML data. For example, you may need to add new nodes or delete existing nodes. Let us look at editing operations for these nodes.

Manipulating Node Values

This is very simple. You already know how to specify a node, so all you need to do is assign a new value to that specified node. For example, if you want to change the value of <name> in the first <data> from the previous XML data, do the following.

$xml->data[0]->name = "value";

This changes the <name> value of the first, that is, the zeroth, <data> to the new value. If there is no node named name, it is added. A method called addChild will be explained later, but in fact, if you specify a node name and set a value like this, you can add a node without using addChild.

Adding Nodes

To add a node, call the addChild method on the node to which it will be added. It looks like this.

node->addChild(name, value);

Specify the node name as the first argument and the value to set as the second argument. You can also specify a namespace as the third argument to specify the place where the node should be added, such as adding a node to a certain namespace. In any case, if you know the first and second arguments, you can add nodes easily.

For example, if you want to add a node named <maker> to the first <data> in the previous XML data, do the following.

$xml->data[0]->addChild('maker', 'microsoft');

Deleting Nodes

There is actually no dedicated method for deletion; you simply unset the property of the corresponding node. For example, if you want to delete the first <data> from the previous XML data, do the following.

unset ($xml->data[o]);

Manipulating Attributes

Nodes can have attributes in addition to values. For example, in a tag such as <data>hoge</data>, "hoge" is the value. Separately, XML can specify attributes inside the <data> tag, as in <data name="a">hoge</data>.

Attributes can be easily added with the addAttribute method.

node->addAttribute(name, value);

That is all you need to do. It is very simple, but there are no dedicated methods for updating or deleting other values. You can extract all attributes contained in a node as a SimpleXMLElement with the attributes method, but SimpleXML does not provide functionality for deleting existing attributes.

 

Now let us look at an example that manipulates nodes and attributes.

<?php
$arr = array('one', 'two', 'three');
$xml = new SimpleXMLElement("data.xml", 0, true);
for($i = 0; $i < count($arr); $i++) {
    $xml->data[$i]->version = $arr[$i];
    unset($xml->data[$i]->price);
    $newdata = $xml->addChild("otherdata", "");
    $newdata->addAttribute("number", $i + 1);
    $newdata->addChild("name", $arr[$i]);
}
header('content-type:text/xml;charset=utf-8');
echo $xml->asXML();

This loads the previous data.xml file, edits its contents, adds new nodes, and outputs it as XML data. Here, it changes the <version> value in <data> and removes <price>. It also creates a new node named <otherdata> and adds a number attribute and a name node.

The modified SimpleXMLElement is output as XML source code using the asXML method. If this asXML method is called without arguments, it returns the XML source code as a text value. It is convenient for displaying the modified XML contents as-is.