Zend_Config_Xml.xml 6.5 KB

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