Source for file xml.php

Documentation is available at xml.php

  1. <?php
  2. /**
  3.  * Using QueryPath.
  4.  *
  5.  * This file contains an example of how QueryPath can be used
  6.  * to generate XML.
  7.  *
  8.  * QueryPath's ability to handle arbitrary XML comes in handy. Fragments of HTML
  9.  * can be composed as external XML documents, and then inserted selectively into
  10.  * an HTML document as needed. Just remember: Ever XML document -- even just a
  11.  * string -- needs to begin with the XML declaration:
  12.  * <code>
  13.  * <? xml version="1.0"?>
  14.  * </code>
  15.  * (A space was inserted above to prevent the documentation renderer from
  16.  * misinterpreting it.)
  17.  * @package Examples
  18.  * @author M Butcher <matt@aleph-null.tv>
  19.  * @license LGPL The GNU Lesser GPL (LGPL) or an MIT-like license.
  20.  */
  21.  
  22. require_once '../src/QueryPath/QueryPath.php';
  23.  
  24.  
  25. // Create a new XML document wrapped in a QueryPath.
  26. // By default, it will point to the root element,
  27. // <author/>
  28. $record qp('<?xml version="1.0"?><author></author>')
  29.   // Add a new last name inside of author.
  30.   ->append('<lastName>Dostoyevsky</lastName>')
  31.   // Select all of the children of <author/>. In this case,
  32.   // that is <lastName/>
  33.   ->children()
  34.   // Oh, wait... we wanted last name to be inside of a <name/> 
  35.   // element. Use wrap to wrap the current element in something:
  36.   ->wrap('<name/>')
  37.   // And before last name, we want to add first name.
  38.   ->before('<firstName/>')
  39.   // Select first name
  40.   ->prev()
  41.   // Set the text of first name
  42.   ->text('Fyodor')
  43.   // And then after first name, add the patronymic
  44.   ->after('<patronymic>Fyodorovich</patronymic>')
  45.   // Now go back to the root element, the top of the document.
  46.   ->top()
  47.   // Add another tag -- origin.
  48.   ->append('<origin>Russia</origin>')
  49.   // turn the QueryPath contents back into a string. Since we are 
  50.   // at the top of the document, the whole document will be converted
  51.   // to a string.
  52.   ->xml();
  53.  
  54. // Print our results.
  55. print $record;

Documentation generated on Sun, 25 Jul 2010 16:09:12 -0500 by phpDocumentor 1.4.3