How to delete xml Dom document in php -
How to delete xml Dom document in php -
i'd search problem , find questions didn't mention error...
i'm trying remove kid of dom document , when type $x->removechild($key);
function, nil happend...
$xmlreq = new domdocument; $xmlreq->loadxml($xmlstr); $x = $xmlreq->getelementsbytagname('*'); foreach($x $key) { if (substr($key->nodevalue,0,3)=="{{{" , substr($key->nodevalue,-3)=="}}}") { $field = explode("|",substr($key->nodevalue,3,strlen($key->nodevalue)-6)); if((int)$field[3]==0) { if(trim($_post[$field[2]])=="") { $x->removechild($key); }else{ $key->nodevalue = trim($_post[$field[2]]); } }elseif((int)$field[3]==1) { if(trim($_post[$field[2]])=="") { $errors.=""; }else{ $key->nodevalue = trim($_post[$field[2]]); } }else{ } } } header("content-type: application/xml"); print $xmlreq->savexml();
and xml:
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0"> <command> <check> <contact:check xmlns:contact="http://epp.nic.ir/ns/contact-1.0"> <contact:id>ghhg-ghgh</contact:id> <contact:id>45</contact:id> <contact:id>45</contact:id> <contact:id>45</contact:id> <contact:authinfo> <contact:pw>1561651321321</contact:pw> </contact:authinfo> </contact:check> </check> <cltrid>test-12345</cltrid> </command> </epp>
and want delete 1 of <contact:id>45</contact:id>
your loop nil since outer conditional looking node nodevalue
starts {{{
, ends }}}
:
foreach($x $key) { if (substr($key->nodevalue,0,3)=="{{{" , substr($key->nodevalue,-3)=="}}}")
additionally, there's no removechild()
method in domnodelist. want fetch node's parent first , phone call removechild() method instead.
a possible alternative:
$x = $xmlreq->getelementsbytagname('*'); $remove = true; foreach($x $key) { if( $key->nodename=='contact:id' && $key->nodevalue=='45' ){ if($remove){ $key->parentnode->removechild($key); $remove = false; } } }
php xml dom
Comments
Post a Comment