XML
|
|
|
|
<? /**
|
| class.xmlconfig.php -- Class used to convert XML document into
| a native PHP array, which is then used to retrieve values.
|
| License: GPL
|
| Written by Serge Stepanov (serge_AT_gfxcafe.com).
| Feel free to email me with questions. If you find this useful,
| would be great to hear from you.
|
| Requirements: PHP 4.0+ and DOM XML
|
| version 2.0 -
| * Complete re-write of XML parsing with more optimized (and logical)
| DOM structure. Takes a small amount of load off get() method, but
| puts more load onto the Parse() method -- with caching
| (highly recommended), this is will provide optimal performance.
| * Root of XML document no longer stored in array.
| When the get() method is passed an XPath, it strips the first "/" at the
| beggining of the variable so that explode() does not create a blank key
| at location "0".
| * get() method no longer checks if the result is empty, it just returns what it gets
| version 1.1 -
| * Bug Fix: Converting of XML to DOM object made compatible for PHP versions
| below 4.2.0.
| version 1.0 -
| * First release.
|
| Example
| ---------------------------------------
| [configuration.xml]
| <config>
| <directories>
| <root>/web/dir</root>
| <backup>/web/backup</backup>
| </directories>
| <!--
| You can create a numerated array
| by using a group of <value> tags inside
| any other tag
| -->
| <listfoo>
| <value>Foo 1</value>
| <value>Foo 2</value>
| </listfoo>
| </config>
|
| [application.php]
| include("class.xmlconfig.php");
|
| $config = new XMLConfig;
| $config->Parse("configuration.xml");
|
| echo $config->get("/directories/root"); // will print '/web/dir'
| print_r($config->get("/listfoo")); // will print array with 'Foo 1' and 'Foo 2' as values
|
**/
class XMLConfig {
var $_XMLArray;
/* Constructor */
function XMLConfig() {
}
/* Convert XML file to DOM Tree */
function Parse($XML) {
if (function_exists("domxml_open_file")) {
$openFile = "domxml_open_file";
} else if (function_exists("xmldocfile")) {
$openFile = "xmldocfile";
} else {
die("XMLConfig: DOM XML not found.");
}
$tree = $openFile($XML);
$root = $tree->root();
$this->convertXMLToArray($root, $this->_xmlArray);
}
/*
Document root to be used.
Currently has no real implications, but is still here for future use.
*/
function setDocument($root) {
$this->_documentRoot = trim($root);
}
/* Transforms path to array placement */
function get($path) {
/* Update counter, for debugging reasons */
$this->_getCounter++;
/* Prepare the XPath string */
$path = trim($path);
if (substr($path, 0, 1) == "/") {
$path = substr($path, 1);
}
/* Seperate by forward slashes */
$path = explode("/", $path);
/* Generate "array"-like resource */
$i = 0;
while ($i < count($path)) {
$getPath .= "['" . $path[$i] . "']";
$i++;
}
/* Evaluate as PHP and set result ro $resultVar */
eval("$resultVar =& $this->_xmlArray$getPath;");
return $resultVar;
}
/*
Alias to get() method, temporary for
backwards compatibility
*/
function getData($path) {
return $this->get($path);
}
/* Converts DOM XML tree to array */
function convertXMLToArray($branch, &$array) {
$branch = $branch->first_child();
while ($branch) {
if ($branch->type == XML_ELEMENT_NODE) {
/* If a content node, get contents */
if ($branch->has_child_nodes()) {
$next = $branch->first_child();
$content = $next->get_content();
/* If it's an empty mode, it's not a text node */
if (!$next->is_blank_node()) {
/* If the name of the node is "value" create incremented array */
if ($branch->node_name() == "value") {
$array[$branch->node_name()][] = $next->get_content();
} else {
$array[$branch->node_name()] = $next->get_content();
}
}
}
/* Recursive code */
$this->convertXMLToArray($branch, $array[$branch->node_name()]);
}
/* Get next sibling */
$branch = $branch->next_sibling();
}
}
}
?>
|
|
|
Usage Example
|
Example
---------------------------------------
[configuration.xml]
<config>
<directories>
<root>/web/dir</root>
<backup>/web/backup</backup>
</directories>
<!--
You can create a numerated array
by using a group of <value> tags inside
any other tag
-->
<listfoo>
<value>Foo 1</value>
<value>Foo 2</value>
</listfoo>
</config>
[application.php]
include("class.xmlconfig.php");
$config = new XMLConfig;
$config->Parse("configuration.xml");
echo $config->get("/directories/root"); // will print '/web/dir'
print_r($config->get("/listfoo")); // will print array with 'Foo 1' and 'Foo 2' as values
|
|
|
Rate This Script
|
|
|
|