Problems with removeChild DOM - PHP5

I have problems with the removeChild method in a recursive function with PHP5. It works, but not always.
In my program, I write an XML file. The aim of the function rec is to remove some incorrect elements after the writing of the xml file.
....
foreach ($doc->documentElement->childNodes as $chyld) {
rec($chyld);
}

...
function rec($node){
if ($node->tagName=="..."){
$nud=$node->parentNode;
$nud->removeChild($node);
} ...
foreach ($node->childNodes as $chyld) {
rec($chyld);
}
When I run the program, some 'bad' elements are removed but not every incorrect tag. For example,just after the root, the function should remove 2 sibling nodes and just remove one of them.
Could somebody explain me why it doen't work correctly and how I could circumvent the problem ?public function checkAttribute( $node, $value ) {
foreach( $this->dom->getElementsByTagname( $node ) as $item ) {
foreach( $item->attributes as $attrib ){
if ( $attrib->nodeValue == $value ) return $item;
}
}
return false;
}

public function removeNode( $parent, $node, $value, $index = 0 ){
$root = $this->dom->getElementsByTagname( $parent );
$offending = $this->checkAttribute( $node, $value );
if( $offending ) $root->item( $index )->removeChild( $offending );

}


you willneed to full in the gapsThank you for replying.

There is still a problem with this code.

Here is the error:

Uncaught exception 'DOMException' with message 'Not Found Error' in c:\program files\easyphp1-7\www\rut\addboxes.inc:528 Stack trace: #0 c:\program files\easyphp1-7\www\rut\addboxes.inc(471): removeNode(Object(DOMElement)) #1 c:\program files\easyphp1-7\www\rut\addboxes.inc(475): delBr(Object(DOMElement), Object(DOMElement), 0, 0) #2 c:\program files\easyphp1-7\www\rut\addboxes.inc(475): delBr(Object(DOMElement)) #3 c:\program files\easyphp1-7\www\rut\addboxes.inc(475): delBr(Object(DOMElement)) #4 c:\program files\easyphp1-7\www\rut\addboxes.inc(475): delBr(Object(DOMElement)) #5 c:\program files\easyphp1-7\www\rut\addboxes.inc(475): delBr(Object(DOMElement)) #6 c:\program files\easyphp1-7\www\rut\RevXML.inc(1565): delBr(Object(DOMElement)) #7 c:\program files\easyphp1-7\www\rut\RevXMLUI.php(373): ReversiXML(Object(DOMElement)) #8 {main} thrown in c:\program files\easyphp1-7\www\rut\addboxes.inc on line 528

And below is the modified code.
In this example, I load an HTML file and try to remove all the br tags.
Maybe the error comes from a wrong index ?
is it always = to 0 ?
I ve changed the getElementbytagname ($node) into ($node->tagName) as it gave no results before and changed the $this->dom into the name of the xml document ($camel).
I thank u in advance.


function delBr($node){
//-----------------------------------------------
$nomtag = $node->tagName;

if (($nomtag=="Br")||($nomtag=="br")){

$nuud=$node->parentNode;
//$nuud->removeChild($node);
removeNode($nuud,$node,0,0);
}

foreach ($node->childNodes as $chyld) {
delBr($chyld);
}

}


function removeNode( $parent, $nodde, $value, $index = 0 ){
/////////////////////////////////////////////////////////////////////
global $camel;

$roote = $camel->getElementsByTagname( $parent->tagName);

if ($value<>0){
$offending = checkAttribute( $nodde, $value ); }
else{
$offending = checkk($nodde);
}

if( $offending ) {
$roote->item( $index )->removeChild( $offending ); }

}


function checkk( $nodee) {
//////////////////////////////////////////////////////////
global $camel;

foreach( $camel->getElementsByTagname( $nodee->tagName ) as $item ) {
return $item;
}
return false;
}Thank you for replying.

There is still a problem with this code.

Here is the error:

Uncaught exception 'DOMException' with message 'Not Found Error' in c:\program files\easyphp1-7\www\rut\addboxes.inc:528 Stack trace: #0 c:\program files\easyphp1-7\www\rut\addboxes.inc(471): removeNode(Object(DOMElement)) #1 c:\program files\easyphp1-7\www\rut\addboxes.inc(475): delBr(Object(DOMElement), Object(DOMElement), 0, 0) #2 c:\program files\easyphp1-7\www\rut\addboxes.inc(475): delBr(Object(DOMElement)) #3 c:\program files\easyphp1-7\www\rut\addboxes.inc(475): delBr(Object(DOMElement)) #4 c:\program files\easyphp1-7\www\rut\addboxes.inc(475): delBr(Object(DOMElement)) #5 c:\program files\easyphp1-7\www\rut\addboxes.inc(475): delBr(Object(DOMElement)) #6 c:\program files\easyphp1-7\www\rut\RevXML.inc(1565): delBr(Object(DOMElement)) #7 c:\program files\easyphp1-7\www\rut\RevXMLUI.php(373): ReversiXML(Object(DOMElement)) #8 {main} thrown in c:\program files\easyphp1-7\www\rut\addboxes.inc on line 528

And below is the modified code.
In this example, I load an HTML file and try to remove all the br tags.
Maybe the error comes from a wrong index ?
is it always = to 0 ?
I ve changed the getElementbytagname ($node) into ($node->tagName) as it gave no results before and changed the $this->dom into the name of the xml document ($camel).
I thank u in advance.


function delBr($node){
//-----------------------------------------------
$nomtag = $node->tagName;

if (($nomtag=="Br")||($nomtag=="br")){

$nuud=$node->parentNode;
//$nuud->removeChild($node);
removeNode($nuud,$node,0,0);
}

foreach ($node->childNodes as $chyld) {
delBr($chyld);
}

}


function removeNode( $parent, $nodde, $value, $index = 0 ){
/////////////////////////////////////////////////////////////////////
global $camel;

$roote = $camel->getElementsByTagname( $parent->tagName);

if ($value<>0){
$offending = checkAttribute( $nodde, $value ); }
else{
$offending = checkk($nodde);
}

if( $offending ) {
$roote->item( $index )->removeChild( $offending ); }

}


function checkk( $nodee) {
//////////////////////////////////////////////////////////
global $camel;

foreach( $camel->getElementsByTagname( $nodee->tagName ) as $item ) {
return $item;
}
return false;
}Problem solved ! Many thanks!
In fact, when u want to remove several nodes (or replace nodes), u need to use items.
Here is an example (remove Br tags from an HTML file-$doc)

...
$listall=$doc->getElementsByTagname( "*" );
foreach ($listall as $bri){
delBr($kms);
$kms++;
}

function delBr($rty){
global $listall,$kms;

if ($listall->item($rty)->tagName=="Br"){
$nud=$listall->item($rty)->parentNode;
$nud->removeChild($listall->item($rty));
}
}
 
Back
Top