4 * @file classes/xml/XMLCustomWriter.inc.php
6 * Copyright (c) 2000-2009 John Willinsky
7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
9 * @class XMLCustomWriter
12 * @brief Wrapper class for writing XML documents using PHP 4.x or 5.x
15 // $Id: XMLCustomWriter.inc.php,v 1.5 2009/09/23 00:42:47 asmecher Exp $
18 import ('xml.XMLNode');
20 class XMLCustomWriter
{
22 * Create a new XML document.
23 * If $url is set, the DOCTYPE definition is treated as a PUBLIC
24 * definition; $dtd should contain the ID, and $url should contain the
25 * URL. Otherwise, $dtd should be the DTD name.
27 function &createDocument($type = null, $dtd = null, $url = null) {
29 if (class_exists('DOMImplementation')) {
30 // Use the new (PHP 5.x) DOM
31 $impl = new DOMImplementation();
32 // only generate a DOCTYPE if type is non-empty
34 $domdtd = $impl->createDocumentType($type, isset($url)?
$dtd:'', isset($url)?
$url:$dtd);
35 $doc = $impl->createDocument($version, '', $domdtd);
37 $doc = $impl->createDocument($version, '');
39 // ensure we are outputting UTF-8
40 $doc->encoding
= 'UTF-8';
42 // Use the XMLNode class
44 $doc->setAttribute('version', $version);
45 if ($type !== null) $doc->setAttribute('type', $type);
46 if ($dtd !== null) $doc->setAttribute('dtd', $dtd);
47 if ($url !== null) $doc->setAttribute('url', $url);
52 function &createElement(&$doc, $name) {
53 if (is_callable(array($doc, 'createElement'))) $element =& $doc->createElement($name);
54 else $element = new XMLNode($name);
59 function &createTextNode(&$doc, $value) {
61 $value = Core
::cleanVar($value);
63 if (is_callable(array($doc, 'createTextNode'))) $element =& $doc->createTextNode($value);
65 $element = new XMLNode();
66 $element->setValue($value);
72 function &appendChild(&$parentNode, &$child) {
73 if (is_callable(array($parentNode, 'appendChild'))) $node =& $parentNode->appendChild($child);
75 $parentNode->addChild($child);
76 $child->setParent($parentNode);
83 function &getAttribute(&$node, $name) {
84 return $node->getAttribute($name);
87 function &hasAttribute(&$node, $name) {
88 if (is_callable(array($node, 'hasAttribute'))) $value =& $node->hasAttribute($name);
90 $attribute =& XMLCustomWriter
::getAttribute($node, $name);
91 $value = ($attribute !== null);
96 function setAttribute(&$node, $name, $value, $appendIfEmpty = true) {
97 if (!$appendIfEmpty && $value == '') return;
98 return $node->setAttribute($name, $value);
101 function &getXML(&$doc) {
102 if (is_callable(array($doc, 'saveXML'))) $xml =& $doc->saveXML();
104 $xml = $doc->toXml();
109 function printXML(&$doc) {
110 if (is_callable(array($doc, 'saveXML'))) echo $doc->saveXML();
111 else $doc->toXml(true);
114 function &createChildWithText(&$doc, &$node, $name, $value, $appendIfEmpty = true) {
116 if ($appendIfEmpty ||
$value != '') {
117 $childNode =& XMLCustomWriter
::createElement($doc, $name);
118 $textNode =& XMLCustomWriter
::createTextNode($doc, $value);
119 XMLCustomWriter
::appendChild($childNode, $textNode);
120 XMLCustomWriter
::appendChild($node, $childNode);
125 function &createChildFromFile(&$doc, &$node, $name, $filename) {
126 $contents =& FileManager
::readFile($filename);
127 if ($contents === false) return null;