Global Variables/XML
| From: | Duncan Dickinson | Date: | Tue, 25 Apr 2000 06:21:39 +0000 |
| Subject: | Global Variables/XML | ||
| Groups: | php.version4 | ||
| Request: | Send a blank email to php-version4+get-14036@lists.php.net to get a copy of this message | ||
Hi all,
I have been staring at a problem for quite a while now and admit
defeat. I apologise for the length of this posting but I feel I
probably just have a semantic error that I keep overlooking.
I have an XML doc and want to place it into a 'tree' of XML Entities.
I use the expat libraries to walk the doc and place each
entity/element into a new instance of XML_Entity. This instance refers
back to its parent and also contains an array of child entities. The
basic XML I am using is displayed on the page included below.
The problem is that the final tree shows all child nodes having
Element A as their parent, I think I'm stuffing up the global variable
as each call to startElement uses Element A and not the element set in
the previous call.
I am using PHP 4.0RC1 (no Optimizer) on Apache 1.3.9 on Redhat Linux.
Anyone see anything? Maybe using domxml would be a better idea?
Thanks in advance,
duncan
<?php
$current_entity;
function XML_File_startElement($parser, $name, $attrs) {
global $current_entity;
echo "<li>The current element is named " . $current_entity->name;
echo "<ul>";
if (empty($current_entity)) {
$current_entity = new XML_Entity($name);
echo "<li>Creating a new object named " . $current_entity->name;
} else {
echo "<li>Adding child to " . $current_entity->name . ". ";
$current_entity = &$current_entity->addChild($name);
echo "<li>Creating a child named " . $current_entity->name
. " with parent " . $current_entity->parent->name;
}
echo "<li>The current element is NOW named " .
$current_entity->name;
echo "</ul>";
}
function XML_File_endElement($parser, $name){
global $current_entity;
echo "<li>Closing element " . $current_entity->name;
echo "<ul>";
if (is_object($current_entity->parent)) {
echo "<li>Parent found. Moving to element " .
$current_entity->name;
$current_entity = &$current_entity->parent;
} else
echo "<li>No Parent Found";
echo "</ul>";
}
class XML_Entity{
var $name;
var $children = array();
var $parent;
function XML_Entity($name){
$this->name = $name;
}
function addChild($name) {
$child = new XML_Entity($name);
$child->parent = &$this;
$this->children[] = &$child;
return $child;
}
function dumpEntity(){
echo "<ul>";
echo "<li><b>" . $this->name . "</b>n";
echo "<ul>";
if(is_object($this->parent))
echo "<li>Parent Name: " . $this->parent->name;
echo "</ul>";
echo "<li>Children:n";
while(list($k, $v) = each($this->children))
$v->dumpEntity();
echo "</ul>";
echo "<hr>";
}
}
class XML_File {
function XML_File(){
}
function loadFile($file) {
global $current_entity;
if(!file_exists($file))
return false;
if (!($fp = fopen($file, "r")))
return false;
$current_entity = "";
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "XML_File_startElement",
"XML_File_endElement");
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp)))
return false;
}
return $current_entity;
}
}
?>
<h2>Test Page</h2>
<P>File Contents =
<pre>
<?php echo
htmlentities("<a><b><c></c></b></a>")?>
</pre>
<?php
$x = new XML_File();
$e = &$x->loadFile("/tmp/test.xml");
echo "<br><h3>Dump of element tree</h3>";
$e->dumpEntity();
?>