How to Remove Nodes in PHP & XML
PHP uses an internal XML parser class to add, edit and remove XML nodes. An XML node contains a piece of data from the XML data set. The data files increase in size as you continue adding data, so removing unneeded or old nodes cleans up the file and limits the amount of storage space used on the Web server's hard drive.
Instructions
-
-
1
Right-click the PHP file that contains the XML file import and select "Open With." Click the PHP editor in the list of programs.
-
2
Open the file. You use the PHP "fopen" function to open the file, and load the file content into the PHP XML parser. The following code opens a file named "customers.xml":
$xmldoc = "customers.xml";
$file = fopen($xmldoc, "rb")
$parse= new DOMDocument();
$parse->loadXML($file) -
-
3
Load the document XML element. The root node is the main description of the file used at the beginning of the file content. Add the following code to load the root node:
$root = $parse->documentElement;
$node= $root->firstChild; -
4
Delete the node from the file. The following code deletes the XML node and its associated data:
$customer_name = $node->childNodes->item(1);
$node->removeChild($customer_name);
-
1