howtos » xml

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:
  1. //First create an XML document
  2. //The following statement sets XML version 1.0, encoding utf-8
  3. $dom = new DOMDocument('1.0', 'utf-8');
  4.  
  5. //Step 1: create the root node
  6. //Now create the root node and declare the XML namespace
  7. $entry_node = $dom->createElementNS('http://www.w3.org/2005/Atom', 'entry');
  8. //If you need to add additional namespace declarations, do it now
  9. $entry_node->setAttribute('xmlns:gd','http://schemas.google.com/g/2005');
  10.  
  11. //Step 2: create all child nodes
  12. $category_node = $dom->createElement('category');
  13. $category_node->setAttribute('scheme', 'http://schemas.google.com/g/2005#kind');
  14. $category_node->setAttribute('term', 'http://schemas.google.com/g/2005#event');
  15.  
  16. $title_node = $dom->createElement('title', $title);
  17. $title_node->setAttribute('type', 'text');
  18.  
  19. $content_node = $dom->createElement('content', $content);
  20. $content_node->setAttribute('type', 'text');
  21.  
  22. //In some cases the child node may itself be a parent with its own child nodes
  23. //Create the parent node
  24. $author_node = $dom->createElement('author');
  25.  
  26. //Create the child nodes
  27. $author_name_node   = $dom->createElement('name', $author_name);
  28. $author_email_node  = $dom->createElement('email', $author_email);
  29.  
  30. //Add the child nodes to the parent
  31. $author_node->appendChild($author_name_node);
  32. $author_node->appendChild($author_email_node);
  33.  
  34. $transparency_node = $dom->createElement('gd:transparency');
  35. $transparency_node->setAttribute('value', 'http://schemas.google.com/g/2005#event.opaque');
  36.  
  37. $eventstatus_node = $dom->createElement('gd:eventStatus');
  38. $eventstatus_node->setAttribute('value', 'http://schemas.google.com/g/2005#event.confirmed');
  39.  
  40. $where_node = $dom->createElement('gd:where');
  41. $where_node->setAttribute('valueString', $where);
  42.  
  43. $when_node = $dom->createElement('gd:when');
  44. $when_node->setAttribute('startTime', $startTime . '+10:00');
  45. $when_node->setAttribute('endTime', $endTime . '+10:00');
  46.  
  47. //Another child node which is also a parent
  48. $reminder_node = $dom->createElement('gd:reminder');
  49. $reminder_node->setAttribute('minutes', $minutes);
  50.  
  51. $when_node->appendChild($reminder_node);
  52.  
  53. //Step 3: Add child nodes to the parent node
  54. $entry_node->appendChild($category_node);
  55. $entry_node->appendChild($title_node);
  56. $entry_node->appendChild($content_node);
  57. $entry_node->appendChild($author_node);
  58. $entry_node->appendChild($transparency_node);
  59. $entry_node->appendChild($eventstatus_node);
  60. $entry_node->appendChild($where_node);
  61. $entry_node->appendChild($when_node);
  62.  
  63.  
  64. //Append the root node to the document
  65. $dom->appendChild($entry_node);
  66.  
  67. //Return the fully-formed XML representation of the DOMDocument
  68. return $dom->saveXML();