SimpleXMLElement :: __ construct():实体:第3行:解析器错误:期望开始标记,'<'未找到

时间:2016-03-08 21:29:26

标签: php xml logging simplexml

使用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中工作。任何人都可以善意地告诉我出了什么问题吗?

1 个答案:

答案 0 :(得分:0)

您的代码中存在一些错误(下面我将错误显示在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"
    }
  }
}