- Sun, 2011-09-11 20:32
- 0 Comments
XML stands for extensible markup language and is a way to describe text or data in an organized format. XML is popular for systems processing data and is widely used in web services. Many different markup languages have been developed using XML syntax, such as XHTML, RSS, and SOAP.
-
<?xml version="1.0" encoding="UTF-8" ?>
-
<books>
-
<book>
-
<title>The Adventures of Tom Sawyer</title>
-
<author>Mark Twain</author>
-
</book>
-
<book>
-
<title>Adventures of Huckleberry Finn</title>
-
<author>Mark Twain</author>
-
</book>
-
</books>
The above is a simple example of XML. Let's use this example and create the same syntax using PHP. PHP uses the DOM Document class to create XML files.
-
$d = new DomDocument('1.0', 'UTF-8');
-
-
$books = $d->createElement('books');
-
-
$book = $d->createElement('book');
-
-
$title = $d->createElement('title','The Adventures of Tom Sawyer');
-
$book->appendChild($title);
-
-
$author = $d->createElement('author','Mark Twain');
-
$book->appendChild($author);
-
-
$books->appendChild($book);
-
-
$book = $d->createElement('book');
-
-
$title = $d->createElement('title','Adventures of Huckleberry Finn');
-
$book->appendChild($title);
-
-
$author = $d->createElement('author','Mark Twain');
-
$book->appendChild($author);
-
-
$books->appendChild($book);
-
-
$d->appendChild($books);
-
-
echo $d->saveXML();
The above php code will output the XML example shown at the beginning of this article. The createElement method of the DomDocument class creates a new node. The node gets appended to the another node level by using the appendChild method. Althought not shown in the above example, the setAttribute method will add an attribute to a node.
Further Reading
PHP DOM Document






Post new comment