Anbei ein kleiner XML-parser (z.T. von php.net) erweitert um OOPfähigkeit, php5 only
Wers braucht.....
PHP-Code:
<?php
/**
*
* @package XCMS
* @subpackage XCMS-CORE
* @version ID: xml.class.php, v1.0.0 alpha, 08.06.2005 22:03:34
* @author Pohl.Christoph <christoph.pohl@ki-homepage.de>
* @version v1.0.0 alpha
* @license license.html GPL
* @copyright Copyright © 2005, Pohl.Christoph
* XCore intranet is a product by ki-homepage.de.
*/
/**
* Handles the XML input and generation
* @version ID: xml.class.php, v1.0.0 alpha, 08.06.2005 22:03:34
* @author Pohl.Christoph
*/
defined( 'XCMS_VALID' ) or die( 'Direct Access to this location is not allowed.' );
class XcmsXml {
public static $conf_index = array();
public static $conf_val;
/**
* Get contents from XML file and insert into an array
*
* @param string $file
* @param string In which stack should the araay be saved
*/
public static function xml2php($file,$name) {
$xml_parser = xml_parser_create();
if (!($fp = fopen($file, "r"))) {
die("unable to open XML");
}
$contents = fread($fp, filesize($file));
fclose($fp);
xml_parse_into_struct($xml_parser, $contents, $arr_vals, $arr_index);
xml_parser_free($xml_parser);
self::$conf_index[$name] = $arr_index;
self::$conf_val[$name] = $arr_vals;
}
/**
* Get back an value of the XML File
* @param string The name of the data which is given in php2xml
* @param string the name of the indexparameter
* @param string which tag should be used (value or so on....)
* @param int the specialtag (0 = the master, other are cdata and so on....whatch the array)
*/
public static function GetValue($name,$indexname,$what = "value",$specialtag = 0){
return self::$conf_val[$name][self::$conf_index[$name][$indexname][$specialtag]][$what];
}
/**
* Convert php array to XML
*
* @param mixed $array_haystack
* @return string
*/
public static function php2xml($array_haystack) {
$xml = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
if ((!empty($array_haystack)) AND (is_array($array_haystack))) {
foreach ($array_haystack as $xml_key => $xml_value) {
switch ($xml_value["type"]) {
case "open":
$xml .= str_repeat("\t", $xml_value["level"] - 1);
$xml .= "<" . strtolower($xml_value["tag"]);
if(isset($xml_value["attributes"])) $xml .= parseattributes($xml_value["attributes"]);
$xml .= ">\n";
break;
case "complete":
$xml .= str_repeat("\t", $xml_value["level"] - 1);
$xml .= "<" . strtolower($xml_value["tag"]);
if(isset($xml_value["attributes"])) $xml .= parseattributes($xml_value["attributes"]);
$xml .= ">" . $xml_value["value"];
$xml .= "</" . strtolower($xml_value["tag"]);
$xml .= ">\n";
break;
case "close":
$xml .= str_repeat("\t", $xml_value["level"] - 1);
$xml .= "</" . strtolower($xml_value["tag"]);
$xml .= ">\n";
break;
default:
break;
}
}
}
return $xml;
}
/**
* Convert php attributes array to XML
*
* @param mixed $attributes
* @return string
*/
function parseattributes($attributes)
{
$xml = "";
foreach ($attributes as $attribute_key => $attribute_value) {
$xml .= sprintf(' %s="%s"', strtolower($attribute_key), $attribute_value);
}
return $xml;
}
}
//Usage
//Liest die Datei ein, und speichert sie innerhalb der Klasse als array ab
XcmsXml::xml2php("meinordner/meinxmldatei.xml","mein.name");
XcmsXml::xml2php("meinordner/meinxmldatei2.xml","mein.name2");
//z.B. die Version aus der Datei 1 (mein.name) wird so ausgelesen
//Es wird nicht mehr auf die XML zugegriffen sondern nur noch auf das array
echo XcmsXml::GetValue("mein.name","VERSION");
//um auf die attributes zuzugreifen von XCMS-Root z.B. HOST
$Test = XcmsXml::GetValue("mein.name","XCMS-ROOT","attributes");
echo $Test["HOST"];
/**
Mögliche Strings hier
tag = Name des übergeordneten Tags
attributes = Attribute
value = Inhalt zwischen den Tags
type = was für ein Typ (open, close, cdata etc) **/
Der Type kann alternativ noch als 4 te Variable angehängt werden :-)
?>
Das ist die TestXML
HTML-Code:
<?xml version='1.0'?>
<config id="main">
<XCMS-ROOT host="localhost" username="foo" password="bar" db="test">
<version>1.0.0 alpha</version>
</XCMS-ROOT>
</config>