Jiddo
03-30-2007, 07:23 PM
Hello guys!
I got a bit of a problem with a XML parser class, which I wrote about a week ago. (I know very little about class programming in PHP.) Anyways, a friend of mine said that I should create a thread about the problem here on this form, so I just registered.
Now, the thing is that the parser works perfectly fine on my PC (running PHP5), but it does not work on my client's webhotel (running PHP4). At first I got a bunch of error messages when trying to run it there, but I managed to get rid of them kinda. However, it still does not work as it should.
Here is the code:
<?php
class XMLNode {
var $name;
var $children;
var $childCount;
var $parent;
var $attributes;
var $data;
function XMLNode($name, $attributes, $parent) {
$this->name = $name;
$this->children = array();
$this->childCount = 0;
$this->parent = $parent;
$this->attributes = $attributes;
$this->data = array();
if($parent != null) {
$parent->appendChild($this);
}
}
function appendChild($node) {
$arr = $this->children;
$arr[$this->childCount] = $node;
$this->children = $arr;
$this->childCount++;
print($this->childCount . "<br />");
}
function appendData($data) {
array_push($this->data, $data);
}
function getChildren() {
return $this->children;
}
function getAttribute($attribute) {
if(!isset($this->attributes[strtoupper($attribute)]))
return null;
else
return $this->attributes[strtoupper($attribute)];
}
function getParent() {
return $this->parent;
}
function getName() {
return $this->name;
}
function printNode() {
$attributeString = '';
foreach($this->attributes as $key => $value) {
$attributeString .= ' ' . $key . '="' . $value . '"';
}
$dataString = '';
foreach($this->attributes as $tempData) {
$dataString .= $tempData;
}
print("<" . $this->getName() . $attributeString);
if(count($this->children) <= 0 && count($this->data) <= 0) {
print(" /><br />");
return;
} else {
print("><br />");
}
foreach($this->children as $child) {
$child->printNode();
}
foreach($this->data as $data) {
print($data);
}
print("</" . $this->getName() . "><br />");
}
}
class XMLParser {
var $currentNode;
var $rootNode;
function XMLParser($file) {
$xml_parser = xml_parser_create('UTF-8');
$this->setCurrentParserNode($xml_parser);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen($file,"r") or die("Failed to open data file.");
while ($data = fread($fp, 4096)) {
xml_parse($xml_parser, $data, feof($fp)) or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
}
fclose($fp);
xml_parser_free($xml_parser);
$this->rootNode->printNode();
}
function getXMLRootNode() {
return $this->rootNode;
}
function startElement($parser, $name, $attribs) {
$this->currentNode = new XMLNode($name, $attribs, $this->currentNode);
if($this->rootNode == null) {
$this->rootNode = $this->currentNode;
}
}
function endElement($parser, $name) {
$this->currentNode = $this->currentNode->getParent();
}
function characterData($parser, $data) {
if(strlen($data) > 0) {
$this->currentNode->appendData($data);
}
}
function setCurrentParserNode($parser) {
xml_set_object($parser,$this);
}
}
?>
(Be nice. It's pretty much one of my first PHP classes :p)
Anyways. As you see, I have added a few lines for debugging etc. (in appendChild($node)). And this is the output I get while running it on my PC:
1
1
2
1
2
3
4
3
4
5
1
2
6
And this is what I get on the webhotel:
1
1
1
1
1
1
1
1
1
1
1
1
1
And this is from parsing the exact same file, so to me it kinda seems like all the instance variables are emptied or something after the function returns o.O. Now, if you need any more info, just tell me and I'll do my best to get it.
I gotta sleep now tho, so I'll check on this topic tomorrow (been trying to figure this out during the past 3 hours, and I still haven't gotten anywhere...).
Thanks in advance!
Yours, Jiddo.
I got a bit of a problem with a XML parser class, which I wrote about a week ago. (I know very little about class programming in PHP.) Anyways, a friend of mine said that I should create a thread about the problem here on this form, so I just registered.
Now, the thing is that the parser works perfectly fine on my PC (running PHP5), but it does not work on my client's webhotel (running PHP4). At first I got a bunch of error messages when trying to run it there, but I managed to get rid of them kinda. However, it still does not work as it should.
Here is the code:
<?php
class XMLNode {
var $name;
var $children;
var $childCount;
var $parent;
var $attributes;
var $data;
function XMLNode($name, $attributes, $parent) {
$this->name = $name;
$this->children = array();
$this->childCount = 0;
$this->parent = $parent;
$this->attributes = $attributes;
$this->data = array();
if($parent != null) {
$parent->appendChild($this);
}
}
function appendChild($node) {
$arr = $this->children;
$arr[$this->childCount] = $node;
$this->children = $arr;
$this->childCount++;
print($this->childCount . "<br />");
}
function appendData($data) {
array_push($this->data, $data);
}
function getChildren() {
return $this->children;
}
function getAttribute($attribute) {
if(!isset($this->attributes[strtoupper($attribute)]))
return null;
else
return $this->attributes[strtoupper($attribute)];
}
function getParent() {
return $this->parent;
}
function getName() {
return $this->name;
}
function printNode() {
$attributeString = '';
foreach($this->attributes as $key => $value) {
$attributeString .= ' ' . $key . '="' . $value . '"';
}
$dataString = '';
foreach($this->attributes as $tempData) {
$dataString .= $tempData;
}
print("<" . $this->getName() . $attributeString);
if(count($this->children) <= 0 && count($this->data) <= 0) {
print(" /><br />");
return;
} else {
print("><br />");
}
foreach($this->children as $child) {
$child->printNode();
}
foreach($this->data as $data) {
print($data);
}
print("</" . $this->getName() . "><br />");
}
}
class XMLParser {
var $currentNode;
var $rootNode;
function XMLParser($file) {
$xml_parser = xml_parser_create('UTF-8');
$this->setCurrentParserNode($xml_parser);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen($file,"r") or die("Failed to open data file.");
while ($data = fread($fp, 4096)) {
xml_parse($xml_parser, $data, feof($fp)) or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
}
fclose($fp);
xml_parser_free($xml_parser);
$this->rootNode->printNode();
}
function getXMLRootNode() {
return $this->rootNode;
}
function startElement($parser, $name, $attribs) {
$this->currentNode = new XMLNode($name, $attribs, $this->currentNode);
if($this->rootNode == null) {
$this->rootNode = $this->currentNode;
}
}
function endElement($parser, $name) {
$this->currentNode = $this->currentNode->getParent();
}
function characterData($parser, $data) {
if(strlen($data) > 0) {
$this->currentNode->appendData($data);
}
}
function setCurrentParserNode($parser) {
xml_set_object($parser,$this);
}
}
?>
(Be nice. It's pretty much one of my first PHP classes :p)
Anyways. As you see, I have added a few lines for debugging etc. (in appendChild($node)). And this is the output I get while running it on my PC:
1
1
2
1
2
3
4
3
4
5
1
2
6
And this is what I get on the webhotel:
1
1
1
1
1
1
1
1
1
1
1
1
1
And this is from parsing the exact same file, so to me it kinda seems like all the instance variables are emptied or something after the function returns o.O. Now, if you need any more info, just tell me and I'll do my best to get it.
I gotta sleep now tho, so I'll check on this topic tomorrow (been trying to figure this out during the past 3 hours, and I still haven't gotten anywhere...).
Thanks in advance!
Yours, Jiddo.