Zend_Config_Ini | Zend_Config Programmer's Reference Guide | Zend_Config_Writer

# Zend_Config_Xml

Zend_Config_Xml enables developers to store configuration data in a simple XML format and read them via nested object property syntax. The root element of the XML file or string is irrelevant and may be named arbitrarily. The first level of XML elements correspond with configuration data sections. The XML format supports hierarchical organization through nesting of XML elements below the section-level elements. The content of a leaf-level XML element corresponds to the value of a configuration datum. Section inheritance is supported by a special XML attribute named extends, and the value of this attribute corresponds with the section from which data are to be inherited by the extending section.

> Note: Return Type
> Configuration data read into Zend_Config_Xml are always returned as strings. Conversion of data from strings to other types is left to developers to suit their particular needs.

Example #1 Using Zend_Config_Xml

This example illustrates a basic use of Zend_Config_Xml for loading configuration data from an XML file. In this example there are configuration data for both a production system and for a staging system. Because the staging system configuration data are very similar to those for production, the staging section inherits from the production section. In this case, the decision is arbitrary and could have been written conversely, with the production section inheriting from the staging section, though this may not be the case for more complex situations. Suppose, then, that the following configuration data are contained in /path/to/config.xml:

1. <?xml version="1.0"?>
2. <configdata>
3. <production>
4. <webhost>www.example.com</webhost>
5. <database>
6. <adapter>pdo_mysql</adapter>
7. <params>
8. <host>db.example.com</host>
9. <username>dbuser</username>
10. <password>secret</password>
11. <dbname>dbname</dbname>
12. </params>
13. </database>
14. </production>
15. <staging extends="production">
16. <database>
17. <params>
18. <host>dev.example.com</host>
19. <username>devuser</username>
20. <password>devsecret</password>
21. </params>
22. </database>
23. </staging>
24. </configdata>

Next, assume that the application developer needs the staging configuration data from the XML file. It is a simple matter to load these data by specifying the XML file and the staging section:

1. $config = new Zend_Config_Xml('/path/to/config.xml', 'staging');
3. echo $config->database->params->host; // prints "dev.example.com"
4. echo $config->database->params->dbname; // prints "dbname"

Example #2 Using Tag Attributes in Zend_Config_Xml

Zend_Config_Xml also supports two additional ways of defining nodes in the configuration. Both make use of attributes. Since the extends and the value attributes are reserved keywords (the latter one by the second way of using attributes), they may not be used. The first way of making usage of attributes is to add attributes in a parent node, which then will be translated into children of that node:

1. <?xml version="1.0"?>
2. <configdata>
3. <production webhost="www.example.com">
4. <database adapter="pdo_mysql">
5. <params host="db.example.com" username="dbuser" password="secret"
6. dbname="dbname"/>
7. </database>
8. </production>
9. <staging extends="production">
10. <database>
11. <params host="dev.example.com" username="devuser"
12. password="devsecret"/>
13. </database>
14. </staging>
15. </configdata>

The other way does not really shorten the config, but keeps it easier to maintain since you don't have to write the tag name twice. You simply create an empty tag with the value in the value attribute:

1. <?xml version="1.0"?>
2. <configdata>
3. <production>
4. <webhost>www.example.com</webhost>
5. <database>
6. <adapter value="pdo_mysql"/>
7. <params>
8. <host value="db.example.com"/>
9. <username value="dbuser"/>
10. <password value="secret"/>
11. <dbname value="dbname"/>
12. </params>
13. </database>
14. </production>
15. <staging extends="production">
16. <database>
17. <params>
18. <host value="dev.example.com"/>
19. <username value="devuser"/>
20. <password value="devsecret"/>
21. </params>
22. </database>
23. </staging>
24. </configdata>

> Note: XML strings
> Zend_Config_Xml is able to load an XML string directly, such as that retrieved from a database. The string is passed as the first parameter to the constructor and must start with the characters '<?xml':

> 1. $string = <<<EOT
> 2. <?xml version="1.0"?>
> 3. <config>
> 4. <production>
> 5. <db>
> 6. <adapter value="pdo_mysql"/>
> 7. <params>
> 8. <host value="db.example.com"/>
> 9. </params>
> 10. </db>
> 11. </production>
> 12. <staging extends="production">
> 13. <db>
> 14. <params>
> 15. <host value="dev.example.com"/>
> 16. </params>
> 17. </db>
> 18. </staging>
> 19. </config>
> 20. EOT;
> 22. $config = new Zend_Config_Xml($string, 'staging');

> Note: Zend_Config XML namespace
> Zend_Config comes with it's own XML namespace, which adds additional functionality to the parsing process. To take advantage of it, you have to define a namespace with the namespace URI http://framework.zend.com/xml/zend-config-xml/1.0/ in your config root node.
> With the namespace enabled, you can now use PHP constants within your configuration files. Additionally, the extends attribute was moved to the new namespace and is deprecated in the `NULL` namespace. It will be completely removed there in Zend Framework 2.0.

> 1. $string = <<<EOT
> 2. <?xml version="1.0"?>
> 3. <config xmlns:zf="http://framework.zend.com/xml/zend-config-xml/1.0/">
> 4. <production>
> 5. <includePath>
> 6. <zf:const zf:name="APPLICATION_PATH"/>/library</includePath>
> 7. <db>
> 8. <adapter value="pdo_mysql"/>
> 9. <params>
> 10. <host value="db.example.com"/>
> 11. </params>
> 12. </db>
> 13. </production>
> 14. <staging zf:extends="production">
> 15. <db>
> 16. <params>
> 17. <host value="dev.example.com"/>
> 18. </params>
> 19. </db>
> 20. </staging>
> 21. </config>
> 22. EOT;
> 24. define('APPLICATION_PATH', dirname(__FILE__));
> 25. $config = new Zend_Config_Xml($string, 'staging');
> 27. echo $config->includePath; // Prints "/var/www/something/library"

Zend_Config_Ini | Zend_Config Programmer's Reference Guide | Zend_Config_Writer