1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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";
*/
}
}
?>
|