PHP - Delete Xml Element

PHP - Delete XML Element

You can use the DOM classes in PHP. ( http://us3.php.net/manual/en/intro.dom.php ).

You will need to read the XML document into memory, use the DOM classes to do manipulation, and then you can save out the XML as needed (to http or to file).

DOMNode is an object in there that has remove features (to address your question).

It's a little more complicated than SimpleXML but once you get used to it, it's much more powerful

(semi-taken from a code example at php.net)

<?php

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

$thedocument = $doc->documentElement;

//this gives you a list of the messages
$list = $thedocument->getElementsByTagName('message');

//figure out which ones you want -- assign it to a variable (ie: $nodeToRemove )
$nodeToRemove = null;
foreach ($list as $domElement){
$attrValue = $domElement->getAttribute('time');
if ($attrValue == 'VALUEYOUCAREABOUT') {
$nodeToRemove = $domElement; //will only remember last one- but this is just an example :)
}
}

//Now remove it.
if ($nodeToRemove != null)
$thedocument->removeChild($nodeToRemove);

echo $doc->saveXML();
?>

This should give you a little bit of an idea on how to remove the element. It will print out the XML without that node. If you wanted to send it to file, just write the string to file.

Delete XML element by value of child element

I know you wanted to do this with DOMDocument, but if you simply need to delete node, you can do this with SimpleXmlElement and XPath:

$xml = new SimpleXMLElement($xmlString);

$id = 1;

$data = $xml->xpath('//input[@name="id"][.=' . $id . ']/..');

if (isset($data[0])) {
unset($data[0]->{0});
}

Here is working demo.

The magic with unset() is explained here.

Removing an xml node using php

Yes, you can use XPath to find the node that is to be removed. You can use predicates to specifiy the exact elements you're looking for. In your case the predicate can be has a name element and its (string/node) value is 'qwe', like e.g.:

<?php
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->loadxml( getData() );

$xpath = new DOMXPath($doc);
foreach( $xpath->query("/uploads/upload[name='qwe']") as $node) {
$node->parentNode->removeChild($node);
}
$doc->formatOutput = true;
echo $doc->savexml();

function getData() {
return <<< eox
<uploads>
<upload>
<name>asd</name>
<type>123</type>
</upload>
<upload>
<name>qwe</name>
<type>456</type>
</upload>
</uploads>
eox;
}

prints

<?xml version="1.0"?>
<uploads>
<upload>
<name>asd</name>
<type>123</type>
</upload>
</uploads>

Remove xml element node based on index

If your intent is to remove the whole <song> element. Use ->removeChild():

$songs = $doc->getElementsByTagName('song');
$hated_artists = array('AA', 'BB', 'CC');
foreach($songs as $song) {
$artist = $song->getElementsByTagName('artist')->item(0)->nodeValue;
if(in_array($artist, $hated_artists)) { // if this song is sang by one of your hated artists
$song->parentNode->removeChild($song); // remove this song
}
}

Sample Usage

If you just want a simple criteria, removing by your desired key, then just use the foreach key. Example:

$keys_to_be_removed = array(14, 18);
foreach($songs as $key => $song) {
if(in_array($key, $keys_to_be_removed)) {
$song->parentNode->removeChild($song);
}
}

Or just explicitly using indexing to delete without the foreach loop:

// starts at index zero
$first_song = $doc->getElementsByTagName('song')->item(0);
$songs = $doc->documentElement;
$songs->removeChild($first_song);

delete a node of an xml file with php

I figured it out how to do it by looking at this question.
You can go about it by doing this:

<?php
$fild = '../XML/link.xml';
$sxe = simplexml_load_file($file);

$nodeURL = $sxe->xpath('link[@url="http://example.com"]');
$noteToRemove = $nodeURL[0];
unset($noteToRemove[0]);

$sxe->asXML($file);
?>

This would work for the xml file I added above

How to delete xml element if it's empty simplexml

1st) close the tag because now your xml is not loaded by Dom parser

2nd) change this line node->$inst_child->module->removeChild($node); to

   $node->parentNode->removeChild($node);

3rd) add echo to the last line to see the result

Here working code

PHP XML remove element and all children by name

Both DOMElement::getElementsByTagName and DOMXPath::query return a DOMNodeList. Your code seems to be expecting a single DOMNode instead. Try this:

$featureddel = $xpath->query('//featured');
// OR:
// $featuredde1 = $dom->getElementsByTagName('featured');

foreach ($featuredde1 as $node) {
$node->parentNode->removeChild($node);
}

Edit: This exact code works as expected for me (PHP 5.3, Debian Squeeze):

<?php 
$xml = '<root>
<featured>
<title></title>
<tweet></tweet>
<img></img>
</featured>
</root>';
$dom = new DOMDocument();
$dom->loadXML($xml);
$featuredde1 = $dom->getElementsByTagName('featured');

foreach ($featuredde1 as $node) {
$node->parentNode->removeChild($node);
}
echo $dom->saveXML();

The output is:

<?xml version="1.0"?>
<root>

</root>

How to delete xml elements/nodes from xml file larger than available RAM?

There are a couple ways to process large documents incrementally, so that you do not need to load the entire structure into memory at once. In either case, yes, you will need to write back out the elements that you wish to keep and omit those you want to remove.

  1. PHP has an XMLReader implementation of a pull parser. An explanation:

    A pull parser creates an iterator that sequentially visits the various
    elements, attributes, and data in an XML document. Code which uses
    this iterator can test the current item (to tell, for example, whether
    it is a start or end element, or text), and inspect its attributes
    (local name, namespace, values of XML attributes, value of text,
    etc.), and can also move the iterator to the next item. The code can
    thus extract information from the document as it traverses it.

  2. Or you could use the SAX XML Parser. Explanation:

    Simple API for XML (SAX) is a lexical, event-driven interface in which
    a document is read serially and its contents are reported as callbacks
    to various methods on a handler object of the user's design. SAX is
    fast and efficient to implement, but difficult to use for extracting
    information at random from the XML, since it tends to burden the
    application author with keeping track of what part of the document is
    being processed.

A lot of people prefer the pull method, but either meets your requirement. Keep in mind that large is relative. If the document fits in memory, then it will almost always be easier to use the DOM. But for really, really large documents that simply might not be an option.



Related Topics



Leave a reply



Submit