<?php //创建xml
// create doctype
$dom = new DOMDocument(“1.0”);
// create root element
$root = $dom->createElement(“toppings”);
$dom->appendChild($root);
$dom->formatOutput=true;
// create child element
$item = $dom->createElement(“item”);
$root->appendChild($item);
// create text node
$text = $dom->createTextNode(“pepperoni”);
$item->appendChild($text);
// create attribute node
$price = $dom->createAttribute(“price”);
$item->appendChild($price);
// create attribute value node
$priceValue = $dom->createTextNode(“4”);
$price->appendChild($priceValue);
// create CDATA section
$cdata = $dom->createCDATASection(” Customer requests that pizza be
sliced into 16 square pieces “);
$root->appendChild($cdata);
// create PI
$pi = $dom->createProcessingInstruction(“pizza”, “bake()”);
$root->appendChild($pi);
// save tree to file
$dom->save(“order.xml”);
// save tree to string
$order = $dom->save(“order.xml”);
?>
<?php //追加element
$doc = new DOMDocument(‘1.0’, ‘utf-8’);
$doc->load(“bookstore.xml”);
$parent = $doc->getElementsByTagName(“bookstore”)->item(0);
$book = $doc->createElement(“book”);
$book->setAttribute(“genre”,”李赞红”);
$book->setAttribute(“ISBN”,”2-3631-4″);
$title = $doc->createElement(“title”);
$title->appendChild($doc->createTextNode(“CS从入门到精通”));
$book->appendChild($title);
$author = $doc->createElement(“author”);
$author->appendChild($doc->createTextNode(“候捷”));
$book->appendChild($author);
$price = $doc->createElement(“price”);
$price->appendChild($doc->createTextNode(“58.3”));
$book->appendChild($price);
$parent->appendChild($book);
$doc->save(“bookstore.xml”);
?>
Leave A Comment