I have an XML file structured as:
<root>
<featured>
<title></title>
<tweet></tweet>
<img></img>
</featured>
</root>
The element is added dynamically , the user needs the option to remove the element on certain occasions,
I have tried a few variations of code including:
$featureddel = $xpath->query('//featured');
while ( $featureddel->hasChildNodes()){
$featureddel->removeChild($featureddel->childNodes->item(0));
}
which gives me a error:
PHP Fatal error: Call to undefined method DOMNodeList::hasChildNodes()
Ive also tried:
$featureddel= $dom->getElementsByTagName('featured');
$featureddel->parentNode->removeChild($featureddel);
which returns:
PHP Fatal error: Call to a member function removeChild() on a non-object
Both
DOMElement::getElementsByTagNameandDOMXPath::queryreturn aDOMNodeList. Your code seems to be expecting a singleDOMNodeinstead. Try this:Edit: This exact code works as expected for me (PHP 5.3, Debian Squeeze):
The output is: