Friday, 14 December 2012

PHP - XML: Creating XML Document

PHP XML Tips - Part 6: You can make a xml document with easy. Look example:
01    <?php
02   
03    $xml = new DOMDocument("1.0");
04   
05    $root = $xml->createElement("data");
06    $xml->appendChild($root);
07   
08    $id   = $xml->createElement("id");
09    $idText = $xml->createTextNode('1');
10    $id->appendChild($idText);
11   
12    $title   = $xml->createElement("title");
13    $titleText = $xml->createTextNode('"PHP Undercover"');
14    $title->appendChild($titleText);
15   
16   
17    $book = $xml->createElement("book");
18    $book->appendChild($id);
19    $book->appendChild($title);
20   
21    $root->appendChild($book);
22   
23    $xml->formatOutput = true;
24    echo "<xmp>". $xml->saveXML() ."</xmp>";
25   
26    $xml->save("mybooks.xml") or die("Error");
27   
28    ?>

No comments:

Post a Comment