[Q] dom function bug?
[email protected] ("Kim, Sung-Sik")
| Newsgroups | php.version4 |
|---|---|
| Message-ID | <[email protected]> |
<?php
/*************************** START test.php *************************************************/
1: <?php
2: function output_node($node, $level=0) {
3: switch($node->type) {
4: case XML_ELEMENT_NODE:
5: for($i=0; $i<$level; $i++)
6: echo " ";
7: echo "<".$node->name;
8: $attributes = $node->attributes();
9: if(is_array($attributes))
10: for(reset($attributes); $attrname = key($attributes); next($attributes))
11: echo " ".$attrname."=".$node->getattr($attrname);
12: echo ">\n";
13: $children = $node->children();
14: for($i=0; $i < count($children); $i++)
15: output_node($children[$i], $level+1);
16: for($i=0; $i<$level; $i++)
17: echo " ";
18: echo "</".$node->name.">\n";
19: break;
20: case XML_TEXT_NODE:
21: for($i=0; $i<$level; $i++)
22: echo " ";
23: echo $node->content;
24: break;
25: case XML_ENTITY_REF_NODE:
26: echo $node->content;
27: break;
28: case XML_COMMENT_NODE:
29: for($i=0; $i<$level; $i++)
30: echo " ";
31: echo "<!--".$node->content."-->";
32: echo "\n";
33: break;
34: }
35: }
36:
37: function list_attr($node) {
38: $attr = domxml_attributes($node);
39: for(reset($attr); $key = key($attr); next($attr)) {
40: echo $key."=".$attr[$key]."\n";
41: }
42: }
43:
44: $xmlstr = "<?xml version='1.0' standalone='yes'?>
45: <!DOCTYPE chapter SYSTEM '/share/sgml/Norman_Walsh/db3xml10/db3xml10.dtd'
46: [ <!ENTITY sp \"spanish\">
47: ]>
48: <chapter language='en'>
49: <title language='en'>Title</title>
50: <tgroup cols='3'>
51: <tbody>
52: <row>
53: <entry>a2</entry>
54: <entry>c2</entry>
55: </row>
56: </tbody>
57: </tgroup>
58: </chapter> ";
59:
60: /* The following code traverses the xml tree node by node
61: by using the methods of the xmlnode object
62: */
63: echo "Test 1: accessing single nodes from php\n";
64: if(!$dom = xmldoc($xmlstr)) {
65: echo "Error while parsing the document\n";
66: exit;
67: }
68: echo "XML Version: ".$dom->version."\n";
69: echo "Standalone: ".$dom->standalone."\n";
70: $rootnode = $dom->root();
71: output_node($rootnode);
72:
73: ?>
/*************************** END test.php *************************************************/
THIS sampe is file from *test/testdom file of PHP 4.0 packeags*.
(REDHAT 6.1 + APACHE 1.3.12 + PHP 4 DSO + LIBXML2.0)
The following is result of test.php
/*********** RESULT OF test.php *******************************************************/
Test 1: accessing single nodes from php
XML Version: 1.0
Standalone: 1
<chapter> // (language='en') ATTRIBUTE DISAPPEARED ?????
<title> // (language='en') ATTRIBUTE DISAPPEARED ?????
Title </title>
<tgroup> // (cols='3') ATTRIBUTE DISAPPEARED ?????
<tbody>
<row>
<entry>
a2 </entry>
<entry>
c2 </entry>
</row>
</tbody>
</tgroup>
</chapter>
/*********** RESULT OF test.php *******************************************************/
problem #1)
The return value of "node->attributs()" is not ARRAY; ( 8 line of test.php )
if i put var_dump($attributes); at 8 line in test.php, result of var_dump($attributes) is starnge.
problem #2)
if i put "$node->parent()" at 7 line in test.php for test, test.php script is die.
sorry for poor english.
test.php
(application/octet-stream, 2 KB)
<?php
function output_node($node, $level=0) {
switch($node->type) {
case XML_ELEMENT_NODE:
for($i=0; $i<$level; $i++)
echo " ";
$node->parent();
echo "<".$node->name;
$attributes = $node->attributes();
if(is_array($attributes))
for(reset($attributes); $attrname = key($attributes); next($attributes))
echo " ".$attrname."=".$node->getattr($attrname);
echo ">\n";
$children = $node->children();
for($i=0; $i < count($children); $i++)
output_node($children[$i], $level+1);
for($i=0; $i<$level; $i++)
echo " ";
echo "</".$node->name.">\n";
break;
case XML_TEXT_NODE:
for($i=0; $i<$level; $i++)
echo " ";
echo $node->content;
break;
case XML_ENTITY_REF_NODE:
echo $node->content;
break;
case XML_COMMENT_NODE:
for($i=0; $i<$level; $i++)
echo " ";
echo "<!--".$node->content."-->";
echo "\n";
break;
}
}
function list_attr($node) {
$attr = domxml_attributes($node);
for(reset($attr); $key = key($attr); next($attr)) {
echo $key."=".$attr[$key]."\n";
}
}
$xmlstr = "<?xml version='1.0' standalone='yes'?>
<!DOCTYPE chapter SYSTEM '/share/sgml/Norman_Walsh/db3xml10/db3xml10.dtd'
[ <!ENTITY sp \"spanish\">
]>
<chapter language='en'>
<title language='en'>Title</title>
<tgroup cols='3'>
<tbody>
<row>
<entry>a2</entry>
<entry>c2</entry>
</row>
</tbody>
</tgroup>
</chapter> ";
/* The following code traverses the xml tree node by node
by using the methods of the xmlnode object
*/
echo "Test 1: accessing single nodes from php\n";
if(!$dom = xmldoc($xmlstr)) {
echo "Error while parsing the document\n";
exit;
}
echo "XML Version: ".$dom->version."\n";
echo "Standalone: ".$dom->standalone."\n";
$rootnode = $dom->root();
output_node($rootnode);
?>