Open Journal Systems  3.3.0
XMLNode.inc.php
1 <?php
2 
18 class XMLNode {
19 
21  var $name;
22 
24  var $parent;
25 
27  var $attributes;
28 
30  var $value;
31 
33  var $children;
34 
39  function __construct($name = null) {
40  $this->name = $name;
41  $this->parent = null;
42  $this->attributes = array();
43  $this->value = null;
44  $this->children = array();
45  }
46 
51  function getName($includeNamespace = true) {
52  if (
53  $includeNamespace ||
54  ($i = strpos($this->name, ':')) === false
55  ) return $this->name;
56  return substr($this->name, $i+1);
57  }
58 
62  function setName($name) {
63  $this->name = $name;
64  }
65 
69  function &getParent() {
70  return $this->parent;
71  }
72 
76  function setParent(&$parent) {
77  $this->parent =& $parent;
78  }
79 
83  function getAttributes() {
85  }
86 
91  function getAttribute($name) {
92  return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
93  }
94 
99  function setAttribute($name, $value) {
100  $this->attributes[$name] = $value;
101  }
102 
107  $this->attributes = $attributes;
108  }
109 
113  function &getValue() {
114  return $this->value;
115  }
116 
120  function setValue($value) {
121  $this->value =& $value;
122  }
123 
127  function &getChildren() {
129  }
130 
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)) {
141  if ($index == 0) {
142  return $child;
143  } else {
144  $index--;
145  }
146  }
147  unset($child);
148  }
149  $child = null;
150  return $child;
151  }
152 
158  function &getChildValue($name, $index = 0) {
159  $node =& $this->getChildByName($name);
160  if ($node) {
161  $returner =& $node->getValue();
162  } else {
163  $returner = null;
164  }
165  return $returner;
166  }
167 
171  function addChild(&$node) {
172  $this->children[] =& $node;
173  }
174 
179  function &toXml($output = null) {
180  $nullVar = null;
181  $out = '';
182 
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') . "\">";
189  } else {
190  $out .= "<!DOCTYPE " . $this->getAttribute('type') . " SYSTEM \"" . $this->getAttribute('dtd') . "\">";
191  }
192  }
193  }
194 
195  if ($this->name !== null) {
196  $out .= '<' . $this->name;
197  foreach ($this->attributes as $name => $value) {
199  $out .= " $name=\"$value\"";
200  }
201  if ($this->name !== '!--') {
202  $out .= '>';
203  }
204  }
205  $out .= XMLNode::xmlentities($this->value, ENT_NOQUOTES);
206  foreach ($this->children as $child) {
207  if ($output !== null) {
208  if ($output === true) echo $out;
209  else fwrite ($output, $out);
210  $out = '';
211  }
212  $out .= $child->toXml($output);
213  }
214  if ($this->name === '!--') {
215  $out .= '-->';
216  } else if ($this->name !== null) {
217  $out .= '</' . $this->name . '>';
218  }
219  if ($output !== null) {
220  if ($output === true) echo $out;
221  else fwrite ($output, $out);
222  return $nullVar;
223  }
224  return $out;
225  }
226 
227  static function xmlentities($string, $quote_style=ENT_QUOTES) {
228  return htmlspecialchars($string, $quote_style, 'UTF-8');
229  }
230 
231  function destroy() {
232  unset($this->value, $this->attributes, $this->parent, $this->name);
233  foreach ($this->children as $child) {
234  $child->destroy();
235  }
236  unset($this->children);
237  }
238 }
239 
240 
XMLNode\getChildren
& getChildren()
Definition: XMLNode.inc.php:142
XMLNode\$value
$value
Definition: XMLNode.inc.php:42
XMLNode\setValue
setValue($value)
Definition: XMLNode.inc.php:135
XMLNode\setAttribute
setAttribute($name, $value)
Definition: XMLNode.inc.php:114
XMLNode\getValue
& getValue()
Definition: XMLNode.inc.php:128
XMLNode\$parent
$parent
Definition: XMLNode.inc.php:30
XMLNode\destroy
destroy()
Definition: XMLNode.inc.php:246
XMLNode\getChildValue
& getChildValue($name, $index=0)
Definition: XMLNode.inc.php:173
XMLNode\__construct
__construct($name=null)
Definition: XMLNode.inc.php:54
XMLNode\getChildByName
& getChildByName($name, $index=0)
Definition: XMLNode.inc.php:151
XMLNode\setParent
setParent(&$parent)
Definition: XMLNode.inc.php:91
XMLNode\getAttribute
getAttribute($name)
Definition: XMLNode.inc.php:106
XMLNode\setName
setName($name)
Definition: XMLNode.inc.php:77
XMLNode\getParent
& getParent()
Definition: XMLNode.inc.php:84
XMLNode\toXml
& toXml($output=null)
Definition: XMLNode.inc.php:194
XMLNode\$attributes
$attributes
Definition: XMLNode.inc.php:36
XMLNode
Default handler for XMLParser returning a simple DOM-style object. This handler parses an XML documen...
Definition: XMLNode.inc.php:18
XMLNode\$children
$children
Definition: XMLNode.inc.php:48
XMLNode\getName
getName($includeNamespace=true)
Definition: XMLNode.inc.php:66
XMLNode\getAttributes
getAttributes()
Definition: XMLNode.inc.php:98
XMLNode\setAttributes
setAttributes($attributes)
Definition: XMLNode.inc.php:121
XMLNode\addChild
addChild(&$node)
Definition: XMLNode.inc.php:186
XMLNode\$name
$name
Definition: XMLNode.inc.php:24
XMLNode\xmlentities
static xmlentities($string, $quote_style=ENT_QUOTES)
Definition: XMLNode.inc.php:242