How to Remove Empty Tags in PHP DOM

The "removeChild" function in the PHP DOM lets you remove an empty XML tag from a file. The function deletes only the specified child and leaves the rest of the document and XML tags intact. After you finish deleting the node, you save the file to the hard drive.

Instructions

    • 1

      Right-click the PHP file that contains your XML procedures. Click the "Open With" option and click your preferred PHP editor.

    • 2

      Open the document and load the XML information. The following code opens the XML file named "customers.xml":

      $doc = new DOMDocument;
      $doc->load('customers.xml');

      Replace the file with your own that you want to edit.

    • 3

      Point to the element you want to remove. The following code points to the first customer in the list of XML data:

      $customers = $doc->documentElement;
      $customer= $cusotmers->getElementsByTagName('customer')->item(0);

    • 4

      Delete the XML element and save the file. The following code deletes the first customer element and saves the file:

      $deletecustomer = $customer->removeChild($customer);
      echo $doc->saveXML();

Related Searches:

References

Comments

Related Ads

Featured