DOMDocument help
Posted: Thu Nov 05, 2009 7:54 pm
I need to modify some html documents and was hoping I can figure out how to do it using DOMDocument. I need to do three things.
1) change the class of any div that currently has a class of 'footnotehilite' to 'footnote'. that proved to be very easy...
2) find nodes that are of type span with a class of 'footscript' then remove the span but leave everything inside the span. I can find the span in question and change the class like in the previous example. But I have no idea how to get all the elements and text inside the span and replace the span with just what is inside.
3) Find certain nodes and completely remove them. Not having any problem finding the nodes in question and changing attributes but can't delete them. This codes seems like it should work but gives me an error...
The error I get is...
DOMNode::removeChild() [function.DOMNode-removeChild]: Not Found Error in . . .
Any Ideas>
1) change the class of any div that currently has a class of 'footnotehilite' to 'footnote'. that proved to be very easy...
Code: Select all
if($NodeName == 'div' AND $class == 'footnotehilite'){
$element->removeAttribute('class');
$element->setAttribute ('class', 'footnote');
}
3) Find certain nodes and completely remove them. Not having any problem finding the nodes in question and changing attributes but can't delete them. This codes seems like it should work but gives me an error...
Code: Select all
$elements = $doc->getElementsByTagName('*');
if (!is_null($elements)) {
foreach ($elements as $element) {
$NodeName = (string)$element->nodeName;
print "Node: '$NodeName' \n";
$attr = $element->getAttributeNode('class');
if($attr){
$class = (string)$attr->value;
print "Class: '$class' \n";
if($NodeName == 'span' AND $class == 'footscript'){
$doc->removeChild($element);
}
}
}
}
DOMNode::removeChild() [function.DOMNode-removeChild]: Not Found Error in . . .
Any Ideas>