SimpleXMLElement::__construct(): 实体: 第 3 行: 解析器错误 : 需要启动标记,找


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;
    }
}

这是我的日志.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"
    }
  }
}