How to Append Data and Delete Data from a XML file using PHPPHP 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.
Consider the XML file candidate.xml that is given below: <?xml version="1.0" encoding="UTF-8"?> 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 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"?> 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"); //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.
|
|
|||
|
||||
“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”
Copyright - © 2004 - 2019 - All Rights Reserved.