How to Append Data and Delete Data from a XML file using PHP

PHP has become popular as a scripting language for creating dynamic and interactive websites. With the increase in the usage of XML as a way to store and retrieve data there is a need to do these functions in PHP also. There is improved support for XML in PHP. With this support it is easily possible to manipulate the XML files as you want. PHP has many functions to achieve this functionality. We will look at how to append data to an existing data node in an XML file and how to delete some strings.


_______________________________________________


For this purpose we have an appendData function in PHP which can be used to append string at the end of a character node. This node in which data is added is supposed to be a DOM node. We will see an example on how to append data.

Consider the XML file candidate.xml that is given below:

<?xml version="1.0" encoding="UTF-8"?>
<candidate>
<candidatename>Robert</candidatename>
<candidatecity>New York</candidatecity>
<SSN>20072007</SSN>
<organization>XYZ Corp</organization>
</candidate>

In the above XML file we will add another element called 'candidatedesignation' and add the designation text in it.

Consider the php code given below:

<?php
$xdoc = new DomDocument;
$xdoc->Load('C:/php/xml_files/candidate.xml');
$candidate = $xdoc->getElementsByTagName('candidate')->item(0);
$newElement = $xdoc ->createElement('candidatedesignation');
$txtNode = $xdoc ->createTextNode ("Project Manager");
$newElement -> appendChild($txtNode);
$candidate -> appendChild($newElement);
$test = $xdoc->save("C:/php/xml_files/candidate2.xml");
echo "<B>Data Appended<B>"
?>

In the above php code a new domdocument is created and the xml file that needs to the edited is loaded in it. Then the node is got through another function and the text is added to it. For this purpose functions like createElement, getElementsByTagName, and createTextNode are used. Finally the appendChild function is used to append the text to the new element and then the element to the candidate node. Save the file thus edited with the save function. This completes the code to append data to an existing xml file.

The resulting xml file after the data has been appended would be like given below:

<?xml version="1.0" encoding="UTF-8"?>
<candidate>
<candidatename>Robert</candidatename>
<candidatecity>New York</candidatecity>
<SSN>20072007</SSN>
<organization>XYZ Corp</organization>
<candidatedesignation>Project Manager</candidatedesignation>
</candidate>

Suppose you want to delete data from an xml file you can use the deleteData function for this purpose.

For example in the above php code given for appending data, you can use the deleteData function to delete some characters after you have created a text node.

<?php

//Some code goes here...

$txtNode = $xdoc ->createTextNode ("Project Manager");
$txtNode->deleteData(0,8);
$newElement -> appendChild($txtNode);
$candidate -> appendChild($newElement);

//Some code goes here...

?>

The deleteData function takes two parameters. The first one is the starting character position and the second one is the end character position up to which the data has to be deleted. After the deletion the text node would be "Manager" instead of "Project Manager".

Now you have seen how to append data to an xml file and how to delete some characters in the xml file. You can try these codes on your own to see the results in the browser.



_______________________________________________

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

_______________________________________



 

FREE Subscription

Stay Current With the Latest Technology Developments Realted to XML. Signup for Our Newsletter and Receive New Articles Through Email.

Name:

Email:

Note : We never rent, trade, or sell our email lists to anyone. We assure that your privacy is respected and protected.

 

 

Add to My Yahoo!

Visit XML Training Material Guide Homepage

 

 

Copyright - © 2004 - 2017 - All Rights Reserved.