| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- Reviewed: no -->
- <sect1 id="zend.config.adapters.xml">
- <title>Zend_Config_Xml</title>
- <para>
- <classname>Zend_Config_Xml</classname> 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 <code>extends</code>, and the value of this attribute corresponds with the section
- from which data are to be inherited by the extending section.
- </para>
- <note>
- <title>Return Type</title>
- <para>
- Configuration data read into <classname>Zend_Config_Xml</classname> are always returned as strings.
- Conversion of data from strings to other types is left to developers to suit their particular needs.
- </para>
- </note>
- <example id="zend.config.adapters.xml.example.using">
- <title>Using Zend_Config_Xml</title>
- <para>
- This example illustrates a basic use of <classname>Zend_Config_Xml</classname> 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
- <code>/path/to/config.xml</code>:
- </para>
- <programlisting role="xml"><![CDATA[
- <?xml version="1.0"?>
- <configdata>
- <production>
- <webhost>www.example.com</webhost>
- <database>
- <adapter>pdo_mysql</adapter>
- <params>
- <host>db.example.com</host>
- <username>dbuser</username>
- <password>secret</password>
- <dbname>dbname</dbname>
- </params>
- </database>
- </production>
- <staging extends="production">
- <database>
- <params>
- <host>dev.example.com</host>
- <username>devuser</username>
- <password>devsecret</password>
- </params>
- </database>
- </staging>
- </configdata>
- ]]></programlisting>
- <para>
- 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:
- </para>
- <programlisting role="php"><![CDATA[
- $config = new Zend_Config_Xml('/path/to/config.xml', 'staging');
- echo $config->database->params->host; // prints "dev.example.com"
- echo $config->database->params->dbname; // prints "dbname"
- ]]></programlisting>
- </example>
- <example id="zend.config.adapters.xml.example.attributes">
- <title>Using Tag Attributes in Zend_Config_Xml</title>
- <para>
- <classname>Zend_Config_Xml</classname> also supports two additional ways of defining nodes in the configuration. Both make use
- of attributes. Since the <code>extends</code> and the <code>value</code> attributes are reserved keywords
- (the latter one by the 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:
- </para>
- <programlisting role="xml"><![CDATA[
- <?xml version="1.0"?>
- <configdata>
- <production webhost="www.example.com">
- <database adapter="pdo_mysql">
- <params host="db.example.com" username="dbuser" password="secret" dbname="dbname"/>
- </database>
- </production>
- <staging extends="production">
- <database>
- <params host="dev.example.com" username="devuser" password="devsecret"/>
- </database>
- </staging>
- </configdata>
- ]]></programlisting>
- <para>
- 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 <code>value</code>
- attribute:
- </para>
- <programlisting role="xml"><![CDATA[
- <?xml version="1.0"?>
- <configdata>
- <production>
- <webhost>www.example.com</webhost>
- <database>
- <adapter value="pdo_mysql"/>
- <params>
- <host value="db.example.com"/>
- <username value="dbuser"/>
- <password value="secret"/>
- <dbname value="dbname"/>
- </params>
- </database>
- </production>
- <staging extends="production">
- <database>
- <params>
- <host value="dev.example.com"/>
- <username value="devuser"/>
- <password value="devsecret"/>
- </params>
- </database>
- </staging>
- </configdata>
- ]]></programlisting>
- </example>
- <note>
- <title>XML strings</title>
- <para>
- <classname>Zend_Config_Xml</classname> 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 <code>'<?xml':</code>
- </para>
- <programlisting role="xml"><![CDATA[
- $string = <<<EOT
- <?xml version="1.0"?>
- <config>
- <production>
- <db>
- <adapter value="pdo_mysql"/>
- <params>
- <host value="db.example.com"/>
- </params>
- </db>
- </production>
- <staging extends="production">
- <db>
- <params>
- <host value="dev.example.com"/>
- </params>
- </db>
- </staging>
- </config>
- EOT;
- $config = new Zend_Config_Xml($string, 'staging');
- ]]></programlisting>
- </note>
- </sect1>
- <!--
- vim:se ts=4 sw=4 et:
- -->
|