create_text_node("\n "); #$node->append_child($blank); $tag= $doc->createElement($tagname); $text= $doc->createTextNode($contents); $tag->appendChild($text); $node->appendChild($tag); } function insertNewBook($doc,$id) { $newbook= $doc->createElement("book"); $newbook->setAttribute("id","3"); addTag($doc,$newbook,"isbn","1-123-12345-1"); addTag($doc,$newbook,"title","Writing in Pencil"); addTag($doc,$newbook,"author","James, Michael"); addTag($doc,$newbook,"author","John, Olivia"); $blank= $doc->createTextNode("\n"); $newbook->appendChild($blank); $root= $doc->documentElement; $root->appendChild($newbook); $blank= $doc->createTextNode("\n"); $root->appendChild($blank); } function showTag($node) { $label= $node->nodeName; $contents= $node->firstChild->nodeValue; print "   $label: $contents
\n"; } function showBook($book) { $id= $book->getAttribute("id"); print "book id=\"$id\"
\n"; foreach ($book->childNodes as $tag) if ($tag->nodeName!="#text") showTag($tag); print "endbook
\n"; } function displayBooks1($doc) { echo "
";
  $xmlfile = $doc->saveXML();
  echo htmlentities($xmlfile);
  echo "
"; } function displayBooks2($doc) { $root= $doc->documentElement; //$children= $root->childNodes(); foreach ($root->childNodes as $child) if ($child->nodeName!="#text") showBook($child); } ?>

Demo Some XML DOM from PHP v5.x

load('booklist.xml'); insertNewBook($doc,"3"); displayBooks1($doc); displayBooks2($doc); $doc->save("booklist.xml"); ?>