-
PHP XML DOMDocument
I am trying to save and load xml files (including append etc) using php in my php file here:
Code:
<?php
$xmlclass = new xml();
class xml{
private $file;
private $xml;
public function getfile(){
return $this->file;
}
public function setfile($name){
$this->file = $name;
}
private function readyXML(){
$this->xml = new DOMDocument();
}
public function createXML(){
$this->readyXML();
$this->saveXML();
}
public function addElement($node, $text){
$this->loadXML();
$ele = $this->xml->createElement($node);
$ele->nodeValue = $text;
$this->xml->appendChild($ele);
$this->saveXML();
}
public function removeElement(){
$this->loadXML();
}
public function countElement($node){
$this->loadXML();
return $this->xml->getElementsByTagName($node)->length;
}
private function loadXML(){
$this->readyXML();
$this->xml->loadXML($this->file);
}
private function saveXML(){
$this->xml->save($this->file);
}
}
?>
Calling the code here:
Code:
<?php
require 'php/xml.php';
$file = 'xml/test.xml';
$xmlclass->setfile($file);
if(!file_exists($xmlclass->getfile()))
$xmlclass->createXML();
$xmlclass->addElement('message','text testing 1');
$xmlclass->addElement('message','text testing 2');
?>
However, I keep getting errors like this:
Warning: DOMDocument::loadXML(): Start tag expected, '<' not found in Entity, line: 1 in C:\xampp\htdocs\Websites\XML Tester\php\xml.php on line 41
Also it seems that addElement doesnt add the element and just over writes it.