Ciao.
In un mio progetto sto utilizzando
questa soluzione devo dire che mi trovo
benino
Codice PHP:
class FileException extends Exception{}
class Xml{
public $doc= null;
public $xp= null;
public function __construct($fileName){
$this->doc= new DOMDocument();
$this->load($fileName);
}
public function save($fileName){
if(!@$this->doc->save($fileName)){
throw new FileException('Error saving file ['.$fileName.'] in class ['.__CLASS__.']');
}
}
public function xPath(){
$this->xp= new domxpath($this->doc);
}
private function load($fileName){
if(!@$this->doc->load($fileName)) {
throw new FileException('Error loading file ['.$fileName.'] in class ['.__CLASS__.']');
}
}
}
class i18n extends Xml{
private $data = array();
public function __construct($fileName){
parent::__construct($fileName);
$this->setLabel();
}
public function __set($name, $value) {
$this->data[$name]= $value;
}
public function __get($name) {
if (array_key_exists($name, $this->data)) {
return $this->data[$name];
}
}
private function setLabel(){
$root= $this->doc->documentElement;
$nodes= $root->childNodes;
foreach($nodes as $node){
if($node->hasChildNodes()){
$this->{$node->tagName}= $node->firstChild->nodeValue;
}
}
}
}
try{
$i18n= new i18n('/en/form_register.xml');
echo $i18n->username;
echo "
";
echo $i18n->password;
// and so on
}
catch(FileException $e){
echo $e->getMessage();
}
form_register.xml
Codice PHP:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<username>Choose your username</username>
<password>Choose your password</password>
<re_password>Confirm password</re_password>
<email>Your e-mail address</email>
<country>Choose your country</country>
<captcha>Authorize your registration</captcha>
</items>