SimpleXMLElement::__construct(): Entity: line 3: parser error : Start tag expected, '<' not found
SimpleXMLElement::__construct(): Entity: line 3: parser error : Start tag expected, '<' not found
使用 php 在此处进行一个小型 xml 项目。我收到一个我不明白的错误。这是我的情况:使用 log.xml,这是我的记录器代码(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;
}
}
这是我的 log.xml:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<log>
<text>logtext example</text>
<line>1</line>
</log>
</root>
这是我的确切错误:
Warning: SimpleXMLElement::__construct(): Entity: line 3: parser error : Start tag expected, '<' not found in C:\xampp\htdocs\xmlproject\index.php on line 27
我不明白哪里出了问题。 log.xml 显然有一个开始标记 <root>
,我尝试使用 $root
访问它。但它告诉我它看不到开始标记。我在 NetBeans 8.1 中工作。谁能告诉我哪里出了问题?
你的代码有一些错误(下面我将错误显示到 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);
完整的工作代码:
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());
输出:
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"
}
}
}
使用 php 在此处进行一个小型 xml 项目。我收到一个我不明白的错误。这是我的情况:使用 log.xml,这是我的记录器代码(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;
}
}
这是我的 log.xml:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<log>
<text>logtext example</text>
<line>1</line>
</log>
</root>
这是我的确切错误:
Warning: SimpleXMLElement::__construct(): Entity: line 3: parser error : Start tag expected, '<' not found in C:\xampp\htdocs\xmlproject\index.php on line 27
我不明白哪里出了问题。 log.xml 显然有一个开始标记 <root>
,我尝试使用 $root
访问它。但它告诉我它看不到开始标记。我在 NetBeans 8.1 中工作。谁能告诉我哪里出了问题?
你的代码有一些错误(下面我将错误显示到 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);
完整的工作代码:
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());
输出:
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"
}
}
}