Re: remove SimpleXML nodes

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I found sample code. Hope this helps.


       $doc = new DOMDocument;
       if (!is_dir($source_dir)) {
           $logger->fatal("Source directory IN is not found.
Terminating...");
            die("Source directory IN is not found. Terminating...");
       }
       $doc->Load($source_dir . "/" . $xmlfilename);
       $xpath = new DomXPath($doc);

       // Find parent node
       $parent = $xpath->query($parent_path);

       // new node will be inserted before this node
       $next = $xpath->query($next_path);

       // Create the new element
       $contentidelement = $doc->createElement('source_id', $contentid);
       $element = $doc->createElement('fallback', 'true');
       $secondelement = $doc->createElement('fallback_locale',
$originatingLocale);

       // Insert the new element
       $parent->item(0)->insertBefore($contentidelement, $next->item(0));
       $parent->item(0)->insertBefore($element, $next->item(0));
       $parent->item(0)->insertBefore($secondelement, $next->item(0));

       //remove the language_code node.
       $parent->item(0)->removeChild($next->item(0));

       // append new node
       $newNode = $doc->createElement("language_code",
$currentfallbacklocale);
       $parent->item(0)->appendChild($newNode);


--
Anas Mughal





On 8/31/06, Anas Mughal <anasmughal@xxxxxxxxx> wrote:

Yes, I agree. SimpleXML is limited.
Do not expect to find advanced features in SimpleXML.

Regards.






On 8/31/06, Jay Paulson < Jay.Paulson@xxxxxxxxxxxxxx> wrote:
>
> >> So I want to do...
> >>
> >> $xmlDatabase = new SimpleXMLElement($myXML);
> >> foreach ($xmlDatabase as $oneTable)
> >> {
> >>    if ($oneTable['name'] == 'two')
> >>    {
> >>       ///  ---- HERE I WANT TO DELETE THE $oneTable NODE
> >>       unset($oneTable);   // <-- and this doesn't work...
> >>    }
> >> }
> >
> > I tried to do the same a while back and could not figure it out. I
> think
> > that SimpleXML is just that - simple. You should rather try using
> DOMXML
> > functions, or as I did, roll an XML parser and do it like that.
>
> Why can't you just recreate the XML and skip the node you don't want to
> include?  So something like below:
>
> foreach ($xmlDatabase as $oneTable)
> {
>     // skip node you don't want in your final xml output
>     if ($oneTable['name'] != 'two')
>     {
>         ....
>     }
> }
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
Anas Mughal




--
Anas Mughal

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux