name = $name; $this->parent = null; $this->attributes = array(); $this->value = null; $this->children = array(); } /** * @return string */ function getName() { return $this->name; } /** * @param $name string */ function setName($name) { $this->name = $name; } /** * @return XMLNode */ function &getParent() { return $this->parent; } /** * @param $parent XMLNode */ function setParent(&$parent) { $this->parent = &$parent; } /** * @return array all attributes */ function getAttributes() { return $this->attributes; } /** * @param $name string attribute name * @return string attribute value */ function getAttribute($name) { return isset($this->attributes[$name]) ? $this->attributes[$name] : null; } /** * @param $name string attribute name * @param value string attribute value */ function setAttribute($name, &$value) { $this->attributes[$name] = &$value; } /** * @param $attributes array */ function setAttributes($attributes) { $this->attributes = $attributes; } /** * @return string */ function &getValue() { return $this->value; } /** * @param $value string */ function setValue($value) { $this->value = &$value; } /** * @return array this node's children (XMLNode objects) */ function &getChildren() { return $this->children; } /** * @param $name * @param $index * @return XMLNode the ($index+1)th child matching the specified name */ function &getChildByName($name, $index = 0) { foreach ($this->children as $child) { if ($child->getName() == $name) { if ($index == 0) { return $child; } else { $index--; } } } $child = null; return $child; } /** * @param $node XMLNode the child node to add */ function addChild(&$node) { $this->children[] = &$node; } /** * @param $output file handle to write to, or true for stdout, or null if XML to be returned as string * @return string */ function &toXml($output = null) { $nullVar = null; $out = ''; if ($this->parent === null) { // This is the root node. Output information about the document. $out .= "getAttribute('version') . "\" encoding=\"UTF-8\"?>\n"; if ($this->getAttribute('type') != '') { if ($this->getAttribute('url') != '') { $out .= "getAttribute('type') . " PUBLIC \"" . $this->getAttribute('dtd') . "\" \"" . $this->getAttribute('url') . "\">"; } else { $out .= "getAttribute('type') . " SYSTEM \"" . $this->getAttribute('dtd') . "\">"; } } } if ($this->name !== null) { $out .= '<' . $this->name; foreach ($this->attributes as $name => $value) { $value = XMLNode::xmlentities($value); $out .= " $name=\"$value\""; } $out .= '>'; } $out .= XMLNode::xmlentities($this->value, ENT_NOQUOTES); foreach ($this->children as $child) { if ($output !== null) { if ($output === true) echo $out; else fwrite ($output, $out); $out = ''; } $out .= $child->toXml($output); } if ($this->name !== null) $out .= 'name . '>'; if ($output !== null) { if ($output === true) echo $out; else fwrite ($output, $out); return $nullVar; } return $out; } function xmlentities($string, $quote_style=ENT_QUOTES) { return htmlspecialchars($string, $quote_style, 'UTF-8'); } } ?>