SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP + DOM replace node

    hey all, i just want to know why i can't replace a node into an xml tree. here's my code which drives me crazy.

    note: i've never used the dom extension ever. is there any hidden information? why the php manual is so confusing on this?

    Code:
     $q = "<component><name>test</name><some_element>this is the content</some_element></component>";
     //New Dom Document
     $dom = new DOMDocument();
     $dom->loadXML($xml);
     //new xpath for the query
     $xp = new DOMXPath($dom);
     foreach ($xp->query("//component[name='test']") as $node){
       //Load XML into a new DOM Document object
      $rp = DOMDocument::loadXML($q);
      for( $child = $rp->documentElement->firstChild; $child; $child = $child->nextSibling )
        $node->parentNode->replaceChild( $dom->importNode( $child ), $node);	
    }
    I also tried the insertBefore() and replaceChild() method but none of them seem to work.

    Thanks in advance,

    Fotis

  2. #2
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


Tags for this Thread

Bookmarks

Posting Permissions

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