4 * @file classes/xml/XMLNode.inc.php
6 * Copyright (c) 2000-2009 John Willinsky
7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
12 * @brief Default handler for XMLParser returning a simple DOM-style object.
13 * This handler parses an XML document into a tree structure of XMLNode objects.
16 // $Id: XMLNode.inc.php,v 1.9 2009/09/23 00:42:47 asmecher Exp $
20 /** @var string the element (tag) name */
23 /** @var XMLNode reference to the parent node (null if this is the root node) */
26 /** @var array the element's attributes */
29 /** @var string the element's value */
32 /** @var array references to the XMLNode children of this node */
37 * @param $name element/tag name
39 function XMLNode($name = null) {
42 $this->attributes
= array();
44 $this->children
= array();
48 * @param $includeNamespace boolean
51 function getName($includeNamespace = true) {
54 ($i = strpos($this->name
, ':')) === false
56 return substr($this->name
, $i+
1);
62 function setName($name) {
69 function &getParent() {
74 * @param $parent XMLNode
76 function setParent(&$parent) {
77 $this->parent
=& $parent;
81 * @return array all attributes
83 function getAttributes() {
84 return $this->attributes
;
88 * @param $name string attribute name
89 * @return string attribute value
91 function getAttribute($name) {
92 return isset($this->attributes
[$name]) ?
$this->attributes
[$name] : null;
96 * @param $name string attribute name
97 * @param value string attribute value
99 function setAttribute($name, $value) {
100 $this->attributes
[$name] = $value;
104 * @param $attributes array
106 function setAttributes($attributes) {
107 $this->attributes
= $attributes;
113 function &getValue() {
118 * @param $value string
120 function setValue($value) {
121 $this->value
=& $value;
125 * @return array this node's children (XMLNode objects)
127 function &getChildren() {
128 return $this->children
;
134 * @return XMLNode the ($index+1)th child matching the specified name
136 function &getChildByName($name, $index = 0) {
137 if (!is_array($name)) $name = array($name);
138 foreach ($this->children
as $key => $junk) {
139 $child =& $this->children
[$key];
140 if (in_array($child->getName(), $name)) {
154 * Get the value of a child node.
155 * @param $name String name of node
156 * @param $index Optional integer index of child node to find
158 function &getChildValue($name, $index = 0) {
159 $node =& $this->getChildByName($name);
161 $returner =& $node->getValue();
169 * @param $node XMLNode the child node to add
171 function addChild(&$node) {
172 $this->children
[] =& $node;
176 * @param $output file handle to write to, or true for stdout, or null if XML to be returned as string
179 function &toXml($output = null) {
183 if ($this->parent
=== null) {
184 // This is the root node. Output information about the document.
185 $out .= "<?xml version=\"" . $this->getAttribute('version') . "\" encoding=\"UTF-8\"?>\n";
186 if ($this->getAttribute('type') != '') {
187 if ($this->getAttribute('url') != '') {
188 $out .= "<!DOCTYPE " . $this->getAttribute('type') . " PUBLIC \"" . $this->getAttribute('dtd') . "\" \"" . $this->getAttribute('url') . "\">";
190 $out .= "<!DOCTYPE " . $this->getAttribute('type') . " SYSTEM \"" . $this->getAttribute('dtd') . "\">";
195 if ($this->name
!== null) {
196 $out .= '<' . $this->name
;
197 foreach ($this->attributes
as $name => $value) {
198 $value = XMLNode
::xmlentities($value);
199 $out .= " $name=\"$value\"";
203 $out .= XMLNode
::xmlentities($this->value
, ENT_NOQUOTES
);
204 foreach ($this->children
as $child) {
205 if ($output !== null) {
206 if ($output === true) echo $out;
207 else fwrite ($output, $out);
210 $out .= $child->toXml($output);
212 if ($this->name
!== null) $out .= '</' . $this->name
. '>';
213 if ($output !== null) {
214 if ($output === true) echo $out;
215 else fwrite ($output, $out);
221 function xmlentities($string, $quote_style=ENT_QUOTES
) {
222 return htmlspecialchars($string, $quote_style, 'UTF-8');
226 unset($this->value
, $this->attributes
, $this->parent
, $this->name
);
227 foreach ($this->children
as $child) {
230 unset($this->children
);