Php 为什么简单XML addChild()会添加3个孩子?
我正在开发用于将错误登录到xml文件的简单类 我所期望的是根元素日志和3个子元素错误以及子元素消息。 但输出是消息1消息2消息3消息1消息2消息3消息1消息2消息3消息1消息2消息3 所有东西都在这里三次了。你知道为什么以及如何重写代码吗 我使用以下代码获得了test.php:Php 为什么简单XML addChild()会添加3个孩子?,php,xml,simplexml,Php,Xml,Simplexml,我正在开发用于将错误登录到xml文件的简单类 我所期望的是根元素日志和3个子元素错误以及子元素消息。 但输出是消息1消息2消息3消息1消息2消息3消息1消息2消息3消息1消息2消息3 所有东西都在这里三次了。你知道为什么以及如何重写代码吗 我使用以下代码获得了test.php: <?php require_once '../meenee/system/Logger.class.php'; $logger = new Logger; $logger->log
<?php
require_once '../meenee/system/Logger.class.php';
$logger = new Logger;
$logger->logErrMessage("Message 1");
$logger->logErrMessage("Message 2");
$logger->logErrMessage("Message 3");
$logger->showErrLog();
检查淋浴日志proc: 每个孩子(有3个孩子) 做 回显文件内容(整个文件内容,其中文件包含3条消息) ->您可以回显文件内容3次
注意:您可能需要将EOL char添加到文件中的单独行。Ou。。以前在foreach还有别的东西。非常感谢你的帮助。
<?xml version="1.0" encoding="UTF-8"?>
<log>
</log>
class Logger{
public $errFile;
public $errXMLLog;
public function __construct(){
$this->errFile = dirname(__FILE__)."/../../error/log.xml";
if(!file_exists($this->errFile)){
throw new WarningException("File does not exists. ".$this->errFile);
}
if(!($this->errXMLLog = simplexml_load_file($this->errFile))){
throw new WarningException("Can't simple xml load file. ".$this->errFile);
}
}
public function logErrMessage($message){
$count = $this->errXMLLog->count();
$this->errXMLLog->addChild('error');
$this->errXMLLog->error[$count]->addChild('message', $message);
$this->errXMLLog->asXml($this->errFile);
}
public function showErrLog(){
foreach ($this->errXMLLog->children() as $child){
echo file_get_contents($this->errFile);
}
}
}