Zend_Config_Xml.xml 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.config.adapters.xml">
  4. <title>Zend_Config_Xml</title>
  5. <para>
  6. <classname>Zend_Config_Xml</classname> enables developers to store configuration data in a
  7. simple XML format and read them via nested object property syntax. The root element of the
  8. XML file or string is irrelevant and may be named arbitrarily. The first level of XML
  9. elements correspond with configuration data sections. The XML format supports hierarchical
  10. organization through nesting of XML elements below the section-level elements. The content
  11. of a leaf-level XML element corresponds to the value of a configuration datum. Section
  12. inheritance is supported by a special XML attribute named <emphasis>extends</emphasis>, and
  13. the value of this attribute corresponds with the section from which data are to be
  14. inherited by the extending section.
  15. </para>
  16. <note>
  17. <title>Return Type</title>
  18. <para>
  19. Configuration data read into <classname>Zend_Config_Xml</classname> are always returned
  20. as strings. Conversion of data from strings to other types is left to developers to
  21. suit their particular needs.
  22. </para>
  23. </note>
  24. <example id="zend.config.adapters.xml.example.using">
  25. <title>Using Zend_Config_Xml</title>
  26. <para>
  27. This example illustrates a basic use of <classname>Zend_Config_Xml</classname> for
  28. loading configuration data from an XML file. In this example there are configuration
  29. data for both a production system and for a staging system. Because the staging system
  30. configuration data are very similar to those for production, the staging section
  31. inherits from the production section. In this case, the decision is arbitrary and could
  32. have been written conversely, with the production section inheriting from the staging
  33. section, though this may not be the case for more complex situations. Suppose, then,
  34. that the following configuration data are contained in
  35. <filename>/path/to/config.xml</filename>:
  36. </para>
  37. <programlisting language="xml"><![CDATA[
  38. <?xml version="1.0"?>
  39. <configdata>
  40. <production>
  41. <webhost>www.example.com</webhost>
  42. <database>
  43. <adapter>pdo_mysql</adapter>
  44. <params>
  45. <host>db.example.com</host>
  46. <username>dbuser</username>
  47. <password>secret</password>
  48. <dbname>dbname</dbname>
  49. </params>
  50. </database>
  51. </production>
  52. <staging extends="production">
  53. <database>
  54. <params>
  55. <host>dev.example.com</host>
  56. <username>devuser</username>
  57. <password>devsecret</password>
  58. </params>
  59. </database>
  60. </staging>
  61. </configdata>
  62. ]]></programlisting>
  63. <para>
  64. Next, assume that the application developer needs the staging configuration data from
  65. the XML file. It is a simple matter to load these data by specifying the XML file and
  66. the staging section:
  67. </para>
  68. <programlisting language="php"><![CDATA[
  69. $config = new Zend_Config_Xml('/path/to/config.xml', 'staging');
  70. echo $config->database->params->host; // prints "dev.example.com"
  71. echo $config->database->params->dbname; // prints "dbname"
  72. ]]></programlisting>
  73. </example>
  74. <example id="zend.config.adapters.xml.example.attributes">
  75. <title>Using Tag Attributes in Zend_Config_Xml</title>
  76. <para>
  77. <classname>Zend_Config_Xml</classname> also supports two additional ways of defining
  78. nodes in the configuration. Both make use of attributes. Since the
  79. <emphasis>extends</emphasis> and the <emphasis>value</emphasis> attributes are reserved
  80. keywords (the latter one by the the second way of using attributes), they may not be
  81. used. The first way of making usage of attributes is to add attributes in a parent
  82. node, which then will be translated into children of that node:
  83. </para>
  84. <programlisting language="xml"><![CDATA[
  85. <?xml version="1.0"?>
  86. <configdata>
  87. <production webhost="www.example.com">
  88. <database adapter="pdo_mysql">
  89. <params host="db.example.com" username="dbuser" password="secret" dbname="dbname"/>
  90. </database>
  91. </production>
  92. <staging extends="production">
  93. <database>
  94. <params host="dev.example.com" username="devuser" password="devsecret"/>
  95. </database>
  96. </staging>
  97. </configdata>
  98. ]]></programlisting>
  99. <para>
  100. The other way does not really shorten the config, but keeps it easier to maintain since
  101. you don't have to write the tag name twice. You simply create an empty tag with the
  102. value in the <emphasis>value</emphasis> attribute:
  103. </para>
  104. <programlisting language="xml"><![CDATA[
  105. <?xml version="1.0"?>
  106. <configdata>
  107. <production>
  108. <webhost>www.example.com</webhost>
  109. <database>
  110. <adapter value="pdo_mysql"/>
  111. <params>
  112. <host value="db.example.com"/>
  113. <username value="dbuser"/>
  114. <password value="secret"/>
  115. <dbname value="dbname"/>
  116. </params>
  117. </database>
  118. </production>
  119. <staging extends="production">
  120. <database>
  121. <params>
  122. <host value="dev.example.com"/>
  123. <username value="devuser"/>
  124. <password value="devsecret"/>
  125. </params>
  126. </database>
  127. </staging>
  128. </configdata>
  129. ]]></programlisting>
  130. </example>
  131. <note>
  132. <title>XML strings</title>
  133. <para>
  134. <classname>Zend_Config_Xml</classname> is able to load an XML string directly,
  135. such as that retrieved from a database. The string is passed
  136. as the first parameter to the constructor and must start with the
  137. characters <emphasis>'&lt;?xml'</emphasis>:
  138. </para>
  139. <programlisting language="xml"><![CDATA[
  140. $string = <<<EOT
  141. <?xml version="1.0"?>
  142. <config>
  143. <production>
  144. <db>
  145. <adapter value="pdo_mysql"/>
  146. <params>
  147. <host value="db.example.com"/>
  148. </params>
  149. </db>
  150. </production>
  151. <staging extends="production">
  152. <db>
  153. <params>
  154. <host value="dev.example.com"/>
  155. </params>
  156. </db>
  157. </staging>
  158. </config>
  159. EOT;
  160. $config = new Zend_Config_Xml($string, 'staging');
  161. ]]></programlisting>
  162. </note>
  163. </sect1>
  164. <!--
  165. vim:se ts=4 sw=4 et:
  166. -->