Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Multi Remove child

    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

    Code:
      <?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

    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");
    Code:
     <?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 !!!
    Last edited by jedistev; 03-11-2009 at 12:00 PM. Reason: Resolved Mutli Remove child

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

    PHP Code:
        $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); 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    jedistev (03-11-2009)

  • #3
    New Coder
    Join Date
    Mar 2009
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    oh it working,

    seems it small, i must be overlook... you are true Savior


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •