2000-01-26 06:22:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
function output_node($node, $level=0) {
|
|
|
|
switch($node->type) {
|
|
|
|
case XML_ELEMENT_NODE:
|
|
|
|
for($i=0; $i<$level; $i++)
|
|
|
|
echo " ";
|
|
|
|
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;
|
2000-02-22 17:03:36 +00:00
|
|
|
break;
|
|
|
|
case XML_ENTITY_REF_NODE:
|
|
|
|
echo $node->content;
|
|
|
|
break;
|
|
|
|
case XML_COMMENT_NODE:
|
|
|
|
for($i=0; $i<$level; $i++)
|
|
|
|
echo " ";
|
|
|
|
echo "<!--".$node->content."-->";
|
2000-01-26 06:22:44 +00:00
|
|
|
echo "\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function list_attr($node) {
|
|
|
|
$attr = domxml_attributes($node);
|
|
|
|
for(reset($attr); $key = key($attr); next($attr)) {
|
|
|
|
echo $key."=".$attr[$key]."\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-14 20:58:10 +00:00
|
|
|
$xmlstr = "<?xml version='1.0' standalone='yes'?>
|
2000-02-22 17:03:36 +00:00
|
|
|
<!DOCTYPE chapter SYSTEM '/share/sgml/Norman_Walsh/db3xml10/db3xml10.dtd'
|
|
|
|
[ <!ENTITY sp \"spanish\">
|
|
|
|
]>
|
2000-03-01 21:16:24 +00:00
|
|
|
<!-- lsfj -->
|
2000-01-26 06:22:44 +00:00
|
|
|
<chapter language='en'><title language='en'>Title</title>
|
2000-02-10 15:24:13 +00:00
|
|
|
<para language='ge'>
|
2000-02-22 17:03:36 +00:00
|
|
|
&sp;
|
|
|
|
<!-- comment -->
|
|
|
|
<informaltable language='&sp;kkk'>
|
2000-01-26 06:22:44 +00:00
|
|
|
<tgroup cols='3'>
|
|
|
|
<tbody>
|
|
|
|
<row><entry>a1</entry><entry morerows='1'>b1</entry><entry>c1</entry></row>
|
|
|
|
<row><entry>a2</entry><entry>c2</entry></row>
|
|
|
|
<row><entry>a3</entry><entry>b3</entry><entry>c3</entry></row>
|
|
|
|
</tbody>
|
|
|
|
</tgroup>
|
|
|
|
</informaltable>
|
|
|
|
</para>
|
|
|
|
</chapter> ";
|
|
|
|
|
2000-02-10 15:24:13 +00:00
|
|
|
/* 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";
|
2000-03-01 21:16:24 +00:00
|
|
|
if(!$dom = xmldoc($xmlstr)) {
|
|
|
|
echo "Error while parsing the document\n";
|
|
|
|
exit;
|
|
|
|
}
|
2000-01-26 06:22:44 +00:00
|
|
|
echo "XML Version: ".$dom->version."\n";
|
2000-03-14 20:58:10 +00:00
|
|
|
echo "Standalone: ".$dom->standalone."\n";
|
2000-02-03 21:12:30 +00:00
|
|
|
$dtd = $dom->dtd();
|
2000-03-02 16:58:07 +00:00
|
|
|
$rootnode = $dom->children();
|
2000-03-02 16:39:50 +00:00
|
|
|
foreach($rootnode as $root)
|
|
|
|
output_node($root);
|
2000-03-02 16:58:07 +00:00
|
|
|
$rootnode = $dom->root();
|
|
|
|
output_node($rootnode);
|
2000-01-26 06:22:44 +00:00
|
|
|
|
2000-02-10 15:24:13 +00:00
|
|
|
/* This one creates a dom tree made of php objects */
|
|
|
|
echo "Test 2: creating a tree with php objects\n";
|
|
|
|
$dom = xmltree($xmlstr);
|
2000-03-03 12:38:20 +00:00
|
|
|
$dom->root->name = "section";
|
2000-02-10 15:24:13 +00:00
|
|
|
var_dump($dom);
|
|
|
|
echo $dom->root->name;
|
|
|
|
echo "\n";
|
2000-02-22 17:03:36 +00:00
|
|
|
/* xmltree() creates a tree which is readonly. This means that a
|
|
|
|
function like
|
|
|
|
$dom->root->new_child("TEST", "ljdf");
|
|
|
|
won't work
|
|
|
|
*/
|
2000-02-10 15:24:13 +00:00
|
|
|
|
|
|
|
/* The following builds a xml document from scratch */
|
|
|
|
echo "Test 3: building a xml document from scratch\n";
|
2000-02-17 15:05:24 +00:00
|
|
|
$doc = new_xmldoc("1.0");
|
|
|
|
$root = $doc->add_root("HTML");
|
|
|
|
$head = $root->new_child("HEAD", "");
|
|
|
|
$head->new_child("TITLE", "Hier der Titel");
|
|
|
|
$body = $root->new_child("BODY", "");
|
|
|
|
$table = $body->new_child("TABLE", "");
|
2000-02-09 12:46:32 +00:00
|
|
|
$table->setattr("WIDTH", "100%");
|
2000-02-17 15:05:24 +00:00
|
|
|
$table->new_child("TR", " ");
|
2000-02-03 21:12:30 +00:00
|
|
|
echo $doc->dumpmem();
|
|
|
|
|
2000-03-14 20:58:10 +00:00
|
|
|
$doc = new TestNode($xmlstr);
|
|
|
|
var_dump($doc);
|
2000-01-26 06:22:44 +00:00
|
|
|
?>
|