working on a small xml project here using php. I am getting an error I do not understand. This is my situation: Using a log.xml, here is my logger code (first thing in index.php):
class logger{
//declare variables
private $logpath = "log.xml";
private $logxml;
//construct the logger
function __construct(){
$this->log("[this logger] starting...", __LINE__);
//execute process
$this->loadxml();
$this->log("log XML file loaded", __LINE__);
//return end result
return $this;
}
//put a log line in the log file with it's file line
public function log($log, $line){
//execute process
$xml = $this->loadxml();
$root = new SimpleXMLElement($xml);//<--- line 27
$newLog = $root.addChild("log");
$newLog.addChild("text", $log);
$newLog.addChild("line", $line);
//memory management
unset($xml);
unset($root);
//return end result
return $newLog;
}
//reload xml
public function loadxml(){
//execute process
$this->logxml = simplexml_load_file($this->logpath) or die("Error: Cannot load xml file: " . $this->logpath);
//return end result
return $this->logxml;
}
//logpath getter
public function logpath(){
return $this->logpath;
}
//logxml getter
public function logxml(){
return $this->logxml;
}
}
Here is my log.xml:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<log>
<text>logtext example</text>
<line>1</line>
</log>
</root>
This is my exact error:
Warning: SimpleXMLElement::__construct(): Entity: line 3: parser error : Start tag expected, '<' not found in C:\xampp\htdocs\xmlproject\index.php on line 27
I do not understand what is going wrong. log.xml clearly has a start tag, <root>
, which I try to access with $root
. Yet it tells me that it cannot see a start tag. I am working in NetBeans 8.1. Can anybody be kind enough to tell me what is going wrong?
You have some errors into your code (below I show errors into public function log
):
$xml = $this->loadxml(); // SimpleXMLElement already
$root = $xml; //new SimpleXMLElement($xml);//<--- line 27
$newLog = $root->addChild("log");
$newLog->addChild("text", $log);
$newLog->addChild("line", $line);
Full working code:
class logger{
//declare variables
private $logpath = "log.xml";
private $logxml;
//construct the logger
function __construct(){
$this->log("[this logger] starting...", __LINE__);
//execute process
$this->loadxml();
$this->log("log XML file loaded", __LINE__);
//return end result
return $this;
}
//put a log line in the log file with it's file line
public function log($log, $line){
//execute process
$xml = $this->loadxml(); // SimpleXMLElement already
$root = $xml; //new SimpleXMLElement($xml);//<--- line 27
$newLog = $root->addChild("log");
$newLog->addChild("text", $log);
$newLog->addChild("line", $line);
//memory management
unset($xml);
unset($root);
//return end result
return $newLog;
}
//reload xml
public function loadxml(){
//execute process
$this->logxml = simplexml_load_file($this->logpath) or die("Error: Cannot load xml file: " . $this->logpath);
//return end result
return $this->logxml;
}
//logpath getter
public function logpath(){
return $this->logpath;
}
//logxml getter
public function logxml(){
return $this->logxml;
}
}
$logger = new logger();
var_dump($logger->logxml());
Output:
object(SimpleXMLElement)#2 (1) {
["log"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#3 (2) {
["text"]=>
string(15) "logtext example"
["line"]=>
string(1) "1"
}
[1]=>
object(SimpleXMLElement)#4 (2) {
["text"]=>
string(19) "log XML file loaded"
["line"]=>
string(2) "14"
}
}
}