Archive for category xml
Creating an XML document with PHP 5’s DOM Functions
PHP 5's DOM Functions (or extension) allows you to create and manipulate XML documents. In fact, it can also be used to create and manipulate any documents adhering to the DOM3 specifications such as HTML and XHTML documents.
Creation of a DOM document is a fairly simple affair
- Instantiate DOMDocument - also specify which version of XML the document is in and how it is encoded
- Create elements through the DOMDocument object
- Set your elements' properties (e.g. values, attributes, child nodes)
- Append the elements to your parent DOMDocument
- Output as fully-formed XML using
DOMDocument::saveXML()
The following example shows you how to create an XML document (using UTF-8 encoding) using PHP 5's DOM functions.
PHP:
-
//First create an XML document
-
//The following statement sets XML version 1.0, encoding utf-8
-
$dom = new DOMDocument('1.0', 'utf-8');
-
-
//Step 1: create the root node
-
//Now create the root node and declare the XML namespace
-
$entry_node = $dom->createElementNS('http://www.w3.org/2005/Atom', 'entry');
-
//If you need to add additional namespace declarations, do it now
-
$entry_node->setAttribute('xmlns:gd','http://schemas.google.com/g/2005');
-
-
//Step 2: create all child nodes
-
$category_node = $dom->createElement('category');
-
$category_node->setAttribute('scheme', 'http://schemas.google.com/g/2005#kind');
-
$category_node->setAttribute('term', 'http://schemas.google.com/g/2005#event');
-
-
$title_node = $dom->createElement('title', $title);
-
$title_node->setAttribute('type', 'text');
-
-
$content_node = $dom->createElement('content', $content);
-
$content_node->setAttribute('type', 'text');
-
-
//In some cases the child node may itself be a parent with its own child nodes
-
//Create the parent node
-
$author_node = $dom->createElement('author');
-
-
//Create the child nodes
-
$author_name_node = $dom->createElement('name', $author_name);
-
$author_email_node = $dom->createElement('email', $author_email);
-
-
//Add the child nodes to the parent
-
$author_node->appendChild($author_name_node);
-
$author_node->appendChild($author_email_node);
-
-
$transparency_node = $dom->createElement('gd:transparency');
-
$transparency_node->setAttribute('value', 'http://schemas.google.com/g/2005#event.opaque');
-
-
$eventstatus_node = $dom->createElement('gd:eventStatus');
-
$eventstatus_node->setAttribute('value', 'http://schemas.google.com/g/2005#event.confirmed');
-
-
$where_node = $dom->createElement('gd:where');
-
$where_node->setAttribute('valueString', $where);
-
-
$when_node = $dom->createElement('gd:when');
-
$when_node->setAttribute('startTime', $startTime . '+10:00');
-
$when_node->setAttribute('endTime', $endTime . '+10:00');
-
-
//Another child node which is also a parent
-
$reminder_node = $dom->createElement('gd:reminder');
-
$reminder_node->setAttribute('minutes', $minutes);
-
-
$when_node->appendChild($reminder_node);
-
-
//Step 3: Add child nodes to the parent node
-
$entry_node->appendChild($category_node);
-
$entry_node->appendChild($title_node);
-
$entry_node->appendChild($content_node);
-
$entry_node->appendChild($author_node);
-
$entry_node->appendChild($transparency_node);
-
$entry_node->appendChild($eventstatus_node);
-
$entry_node->appendChild($where_node);
-
$entry_node->appendChild($when_node);
-
-
-
//Append the root node to the document
-
$dom->appendChild($entry_node);
-
-
//Return the fully-formed XML representation of the DOMDocument
-
return $dom->saveXML();