summaryrefslogtreecommitdiff
path: root/utils/xml.php
diff options
context:
space:
mode:
authordeva <deva>2008-10-04 10:38:03 +0000
committerdeva <deva>2008-10-04 10:38:03 +0000
commitcce5e7710295021b41d9aaecc503a60fb99256be (patch)
tree660235be91fb821e976c7ae62347eb368ce87524 /utils/xml.php
Initial revision
Diffstat (limited to 'utils/xml.php')
-rw-r--r--utils/xml.php126
1 files changed, 126 insertions, 0 deletions
diff --git a/utils/xml.php b/utils/xml.php
new file mode 100644
index 0000000..69059b0
--- /dev/null
+++ b/utils/xml.php
@@ -0,0 +1,126 @@
+<?php
+
+class XMLDOMAttribute
+{
+ public $name;
+ public $value;
+}
+
+class XMLDOMNode
+{
+ public $parentnode;
+ public $name;
+ public $contents;
+ public $attributes = array();
+ public $children = array();
+
+ public function XMLDOMNode(&$parentnode)
+ {
+ $this->parentnode = &$parentnode;
+ }
+
+ public function addNode(&$node)
+ {
+ echo "<p>" . $this->name . "::addNode(" . $node->name . ")</p>";
+ // $node->parent = &$this;
+ array_push($this->children, &$node);
+ }
+
+ public function addAttribute($name, $value)
+ {
+ // echo "<p>" . $this->name . "::addAttribute(" . $name . ", ". $value . ")</p>";
+ $attribute = new XMLDOMAttribute();
+ $attribute->name = strtolower($name);
+ $attribute->value = $value;
+ array_push($this->attributes, &$attribute);
+ }
+
+}
+
+class XMLDOMReader
+{
+ private $parent;
+ private $curnode;
+ public $root;
+
+ private function startThreadElement($parser, $name, $attribs)
+ {
+ $this->parent = &$this->curnode;
+
+ $this->curnode = new XMLDOMNode(&$this->parent);
+ $this->curnode->name = $name;
+
+ while (list($name, $value) = each($attribs)) {
+ $this->curnode->addAttribute($name, $value);
+ }
+
+ $this->parent->addNode(&$this->curnode);
+ }
+
+ private function dataThreadElement($parser, $data)
+ {
+ $this->curnode->contents = $data;
+ }
+
+ private function endThreadElement($parser, $name)
+ {
+ $this->curnode = &$this->parent;
+ }
+
+ public function XMLDOMReader($file)
+ {
+ $doc = new DOMDocument();
+ $doc->load('book.xml');
+ echo $doc->saveXML();
+
+ /*
+ $this->root = new XMLDOMNode(&$this->root);
+ $this->root->name = "root";
+ $this->parent = &$this->root;
+
+ // parse the xml file
+ $xml_parser = xml_parser_create("ISO-8859-1");
+ xml_set_element_handler($xml_parser, "startThreadElement", "endThreadElement");
+ xml_set_character_data_handler($xml_parser, "dataThreadElement");
+ xml_set_object ( $xml_parser, $this );
+
+ $data = file_get_contents($file);
+
+ if (!xml_parse($xml_parser, $data, true)) {
+ die(sprintf("XML error: %s at line %d",
+ xml_error_string(xml_get_error_code($xml_parser)),
+ xml_get_current_line_number($xml_parser)));
+ }
+ xml_parser_free($xml_parser);
+ */
+
+ // $parent;
+ /*
+ $this->root = new XMLDOMNode(&$this->root);
+ $this->root->name = "Root";
+
+ $node0 = new XMLDOMNode(&$this->root);
+ $node0->name = "Node1";
+ array_push($this->root->children, &$node0);
+ // $this->root->addNode(&$node0);
+
+ $node1 = new XMLDOMNode(&$node0);
+ $node1->name = "Node1.1";
+ array_push($node0->children, &$node1);
+ // $node0->addNode(&$node1);
+
+ $node2 = new XMLDOMNode(&$node0);
+ $node2->name = "Node1.2";
+ array_push($node0->children, &$node2);
+ // $node0->addNode(&$node2);
+
+ echo "\n<pre>";
+ echo $this->root . " - " . $node0 . " - " . $node1 . " - " . $node2 . "\n";
+ print_r($this->root);
+ echo "</pre>\n";
+ */
+ }
+}
+
+
+?> \ No newline at end of file