Re: [PHP4BETA] Global Variables/XML
| From: | Ly) Vuong | Date: | Tue, 25 Apr 2000 23:57:48 +0000 |
| Subject: | Re: [PHP4BETA] Global Variables/XML | ||
| References: | 1 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-14053@lists.php.net to get a copy of this message | ||
Hello Duncan,
I was having similar problem, trying to build hierarchy XML tree using
array. However, I managed to make it works using array's pop & push
functions.
I don't have the source codes here with me, but I can send you the
classes, source codes, if you're interest. Here is a simple test:
http://www.irisphoto.com/dev/xml
If this's what you want to construct, let me know.
Cheers,
Ly Vuong
---
Duncan Dickinson wrote:
>
> 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();
> ?>
>
> --
> PHP 4.0 Beta Mailing List <http://www.php.net/version4/>
> To unsubscribe, e-mail: php4beta-unsubscribe@lists.php.net
> For additional commands, e-mail: php4beta-help@lists.php.net
> To contact the list administrators, e-mail: php4beta-admin@lists.php.net