XML
Parses a XML file using XML_Parser.
Option | Data Type | Default value | Description |
---|---|---|---|
version |
string | 1.0 |
The XML version to use. |
encoding |
string | ISO-8859-1 |
The content encoding to use when parsing and storing data. |
name |
string | conf |
As with PHPArray, this defines the name of the global configuration root. |
indent |
string | |
The character used for indentation when writing the XML document, if any. By default, two spaces are used. |
linebreak |
string | \n |
The line-breaking character(s) to use when writing the XML document. |
addDecl |
boolean | TRUE | Controls whether the XML declaration is added to the start of the XML document. |
useAttr |
boolean | TRUE | Controls whether attributes are parsed and saved. |
isFile |
boolean | TRUE | If TRUE, the first argument to parseConfig() will be taken as the file name for the XML file to load. If FALSE, the argument will be taken as the XML data itself and parsed accordingly. |
useCData |
boolean | FALSE | Controls whether data is enclosed in CDATA blocks. |
To utilize the XML container, you need to have the XML_Parser package installed (optional dependency).
Writing a config file with the XML container
<?php
require_once 'Config.php';
$conf = new Config();
$root = $conf->getRoot();
$root->createComment('Demo config file with XML container');
//we need a main section, otherwise we get invalid XML
// see http://pear.php.net/bugs/bug.php?id=18357
$main = $root->createSection('config');
$main->createDirective('openLastFile', true);
$main->createDirective('rememberFiles', 8);
$main->createBlank();
$dbsect = $main->createSection('database');
$dbsect->createDirective('host', 'db-server.example.org');
$dbsect->createDirective('name', 'demo-db');
//this is a nested section
$subsect = $dbsect->createSection('settings');
$subsect->createDirective('charset', 'utf-8');
$subsect->createDirective('reconnect', false);
$r = $conf->writeConfig(
'xml-write.txt',
'xml',
array(
'encoding' => 'UTF-8',
'indent' => ' ',
)
);
if (PEAR::isError($r)) {
echo $r->getMessage() . "\n";
}
?>
Generated file
<?xml version="1.0" encoding="UTF-8"?> <!-- Demo config file with XML container --> <config> <openLastFile>1</openLastFile> <rememberFiles>8</rememberFiles> <database> <host>db-server.example.org</host> <name>demo-db</name> <settings> <charset>utf-8</charset> <reconnect /> </settings> </database> </config>