How to delete element with DOMDocument?
You remove the node by telling the parent node to remove the child:
$href->parentNode->removeChild($href);
See DOMNode::$parentNode
Docs and DOMNode::removeChild()
Docs.
See as well:
- How to remove attributes using PHP DOMDocument?
- How to remove an HTML element using the DOMDocument class
How to remove an HTML element using the DOMDocument class
http://us2.php.net/manual/en/domnode.removechild.php
DomDocument is a DomNode.. You can just call remove child and you should be fine.
EDIT: Just noticed you were probably talking about the page you are working with currently. Don't know if DomDocument would work. You may wanna look to use javascript at that point (if its already been served up to the client)
PHP DOMDocument: Delete elements by class
You need to use the removeChild()
method of the parent element:
$xpath = new DOMXPath($dom);
foreach($xpath->query('//div[contains(attribute::class, "foo")]') as $e ) {
// Delete this node
$e->parentNode->removeChild($e);
}
Btw, about your second question, if there are no elements found, the loop won't iterate at all.
Here comes a working example:
$html = <<<EOF
<div class="main">
<div class="delete_this" contenteditable="true">Target</div>
<div class="class1"></div>
<div class="content"><p>Anything</p></div>
</div>
EOF;
$doc = new DOMDocument();
$doc->loadHTML($html);
$selector = new DOMXPath($doc);
foreach($selector->query('//div[contains(attribute::class, "delete_this")]') as $e ) {
$e->parentNode->removeChild($e);
}
echo $doc->saveHTML($doc->documentElement);
How to remove a specific element from DOMDocument in PHP?
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
foreach ($xpath->evaluate('//input[@name = "guesstext2"]') as $input) {
$input->parentNode->removeChild($input);
}
PHP domDocument remove element by attribute value
Assuming $_GET['id']
is the cidr
value you want to remove, I'd use XPath
$xpath = new DOMXPath($doc);
$nodes = $xpath->query('//node[@cidr="' . $id . '"]')
foreach ($nodes as $node) {
$node->parentNode->removeChild($node);
}
PHP DOMDocument not removing all element
The DOMNodeList
returned by $dom->getElementsByTagName
is "live". So when you remove a script, it's removed from the node list, and all the elements of the list shift their indexes down. Then the for
loop goes to the next index, and it ends up skipping every other element.
Convert the node list to an array first.
foreach (iterator_to_array($dom->getElementsByTagName('script')) as $item) {
$item->parentNode->removeChild($item);
}
PHP DOMDocument : How remove a div?
The removeChild
method removes a child element from a node. A div cannot be a child element of the document
object, only <html>
can be (in an HTML document).
Having found the div you want to remove, you need to fetch its parent node. Then call removeChild
on that.
$divMeta->parentNode->removeChild($divMeta)
Delete root element and all children via PHP DOM Document
Fixing the Code
Several errors in your code:
Your xpath should be
"//person"
instead of just"person"
.$personXPath->evaluate()
(DOMXPath::evaluate) returns DOMNodeList, which is an Interator of all the DOMElement that matches the query. Your code expects it to be a DOMElement that has a hasChildNodes method.
So you may change your code to get proper output without error:
<?php
$xmlDocument=new DOMDocument;
$xmlDocument->preserveWhiteSpace=false;
$xmlDocument->load("person.xml");
$personXPath = new DOMXPath($xmlDocument);
$parentNodes = $personXPath->evaluate('//person');
foreach ($parentNodes as $parentNode) {
while ($parentNode->hasChildNodes()){
$parentNode->removeChild($parentNode->childNodes->item(0));
}
}
echo $xmlDocument->saveXML();
This would effectively remove all child nodes of all <person>
tags.
Output:
<?xml version="1.0"?>
<person/>
Simply remove all <person>
But the code above does not remove the <person>
tags in the XML file. If, for the given XML structure, that you want to remove all <person>
tags, there is no need to remove child elements of any <person>
at all:
<?php
$xmlDocument=new DOMDocument;
$xmlDocument->preserveWhiteSpace=false;
$xmlDocument->load("person.xml");
$personXPath = new DOMXPath($xmlDocument);
$personNodes = $personXPath->evaluate('//person');
foreach ($personNodes as $personNode) {
$personNode->parentNode->removeChild($personNode);
}
echo $xmlDocument->saveXML();
Output:
<?xml version="1.0"?>
Related Topics
How to Implement a Callback in PHP
How to Locate the PHP.Ini File (Xampp)
How to Check If a File Is Mp3 or Image File
PHP Convert Date Format Dd/Mm/Yyyy =≫ Yyyy-Mm-Dd
Parentheses Altering Semantics of Function Call Result
Use One Bind_Param() With Variable Number of Input Vars
How to Get Current Route in Symfony 2
Convert Time and Date from One Time Zone to Another in PHP
Delete Multiple Rows by Selecting Checkboxes Using PHP
Multidimensional Array Iteration
Access Controller Method from Another Controller in Laravel 5
When to Use Strtr VS Str_Replace
Serializing PHP Object to Json
Calculating Days of Week Given a Week Number
PHP Get All the Images from Url Which Width and Height ≫=200 More Quicker
PHP - How to Best Determine If the Current Invocation Is from Cli or Web Server
Why Use a Psr-0 or Psr-4 Autoload in Composer If Classmap Is Actually Faster