...

View Full Version : Resolved Multi Remove child



jedistev
03-11-2009, 12:47 PM
Hi

i have been working PHP for last two weeks i have overcome with it and enjoy every mins ..cos i'm JSP person for many years or AJAX guy :)

there is problem with Remove Child on XML in PHP

i am able to remove one line of Remove Child but cannot do more than one and keep nothing happened

here is example of XML



<?xml version="1.0" ?>
- <msgBody>
<email>test@test.org</email>
<email>test@test.com</email>
<msgExplanation>A non-fatal error occurred on power supply 1</msgExplanation>
<msgSystemAction>msgSystemAction</msgSystemAction>
<msgResponse>test</msgResponse>
<msgOperatorResponse>Dreaming</msgOperatorResponse>
</msgBody>


i wrote code



<?php
$emails = new DOMDocument;
$emails->Load("emails.xml");
$emailList=$emails->getElementsByTagName("msgBody")->item($id); // access the <emails> child
// look in the emails all the children with the tag <email>, choose the one with index 2.
$emailToRemove=$emailList->getElementsByTagName("msgOperatorResponse")->item($id);
$emailToRemove=$emailList->getElementsByTagName("msgExplanation")->item($id);
$emailToRemove=$emailList->getElementsByTagName("msgSystemAction")->item($id);

//we want to remove the children from <emails> so I used $emailList which points to <anytag> and removed the proper child.
$emailList->removeChild($emailToRemove);
$emails->save("emails1.xml");





<?xml version="1.0" ?>
- <msgBody>
<email>test@test.org</email>
<email>test@test.com</email>
<msgExplanation>A non-fatal error occurred on power supply 1</msgExplanation>
<msgResponse>test</msgResponse>
<msgOperatorResponse>Dreaming</msgOperatorResponse>
</msgBody>

and results is remove one line, is MsgSystemAction, nothing else

but i want remove more than one which i need remove msgOperatorResponse, msgExplanation, msgSystemAction

any idea where do i go wrong... your help would be savior !!!:thumbsup:

Fou-Lu
03-11-2009, 12:54 PM
One by one. Best I see, you're only calling removeChild on the $emailList->getElementsByTagName("msgSystemAction")->item($id); object. You cannot append nodes in this fashion.



$emailToRemove=$emailList->getElementsByTagName("msgOperatorResponse")->item($id);
$emailList->removeChild($emailToRemove);
$emailToRemove=$emailList->getElementsByTagName("msgExplanation")->item($id);
$emailList->removeChild($emailToRemove);
$emailToRemove=$emailList->getElementsByTagName("msgSystemAction")->item($id);
$emailList->removeChild($emailToRemove);

jedistev
03-11-2009, 12:59 PM
seems it small, i must be overlook... you are true Savior :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum