XMLang Parser

Wir sammeln alle Infos der Bonusepisode von Pokémon Karmesin und Purpur für euch!

Zu der Infoseite von „Die Mo-Mo-Manie“
  • Hi,


    ich hatte grad langeweile und hab daher in Anlehung an den Syntax BBCodes Thread nen XML Parser geschrieben, damit es eben doch eine funktionierende Sprache auf XML Basis gibt :D, der von euch selbst erweitert werden kann:


    PHP
    <?php/** * Gives basic funtions for creating a xml language *  * @author	TimWolla * @copyright	2009 TimWolla * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> */class XMLangParser {	protected $tree;	protected $programname;	protected $consts = array();	protected $vars = array();	public function __construct($filename) {		if(!file_exists($filename)) throw new Exception('The file '.$file.' doesn\'t exists');		if(!is_readable($filename)) throw new Exception('The file '.$file.' doesn\'t is readable');		$this->tree = simplexml_load_file($filename);		$this->parse();	}	protected function getAtt($from, $name) {		$x = $from->attributes();		return $x[$name][0];	}	protected function parse() {		$this->programname = $this->getAtt($this->tree, 'name');		foreach($this->tree->commands->command as $command) {			$childs = $command->children();			if(count($childs) > 1) throw new Exception('Too many Children for tag "command"');			elseif(count($childs) < 1) throw new Exception('Too less Children for tag "command"');			foreach($childs as $key =>$val) {$function = $key;break;}			if(method_exists($this, $function)) {				$this->$function($command->$function);			}			else {				throw new Exception('Unknown method '.$function);			}		}	}}?>


    Meine erste Funktionssammlung :D

    PHP
    <?phprequire('XMLangParser.class.php');class XMLangParserStd extends XMLangParser {	public function out($tree) {		echo $tree;	}	public function setVar($tree) {		$att = $this->getAtt($tree, 'name');		if(is_null($att)) throw new Exception('Attribute name is missing, for Method setVar');		$this->vars[(string)$att] = (string)$tree;	}	public function setConst($tree) {		$att = $this->getAtt($tree, 'name');		if(is_null($att)) throw new Exception('Attribute name is missing, for Method setConst');		if(!isset($this->consts[(string)$att])) $this->consts[(string)$att] = (string)$tree;		else throw new Exception('Const '.$att.' is already declared');	}}?>


    XML
    <?xml version="1.0" encoding="utf-8"?><program name="Hello World!">	<commands>		<command><out>Hello World!</out></command>		<command><setConst name="test">Hello World!</setConst></command>	</commands></program>


    Benutzung:


    PHP
    <?php new XMLangParser('filename'); ?>


    Den Klassennamen, natürlich durch die benutzte Funktionssammlung ersetzen.



    Viel Spaß, und fröhliches Programmieren :D