cvs: phpdoc /en/reference/simplexml .cvsignore reference.xml /en/reference/simplexml/functions simplexml-element-asXML.xml
simplexml-element-attributes.xml simplexml-element-children.xml simplexml-element-xpath.xml simplexml-load-dom.xml simplexml-load-file.xml
simplexml-load-string.xml

From: Date: Fri, 23 Jan 2004 16:17:18 +0000
Subject: cvs: phpdoc /en/reference/simplexml .cvsignore reference.xml /en/reference/simplexml/functions simplexml-element-asXML.xml
simplexml-element-attributes.xml simplexml-element-children.xml simplexml-element-xpath.xml simplexml-load-dom.xml simplexml-load-file.xml
simplexml-load-string.xml
Groups: php.doc 
Request: Send a blank email to phpdoc+get-969358420@lists.php.net to get a copy of this message
kennyt Fri Jan 23 11:17:18 2004 EDT Added files: /phpdoc/en/reference/simplexml .cvsignore reference.xml /phpdoc/en/reference/simplexml/functions simplexml-element-asXML.xml simplexml-element-attributes.xml simplexml-element-children.xml simplexml-element-xpath.xml simplexml-load-dom.xml simplexml-load-file.xml simplexml-load-string.xml Log: Added new SimpleXML reference.

http://cvs.php.net/co.php/phpdoc/en/reference/simplexml/.cvsignore?r=1.1&p=1 Index: phpdoc/en/reference/simplexml/.cvsignore +++ phpdoc/en/reference/simplexml/.cvsignore functions.xml http://cvs.php.net/co.php/phpdoc/en/reference/simplexml/reference.xml?r=1.1&p=1 Index: phpdoc/en/reference/simplexml/reference.xml +++ phpdoc/en/reference/simplexml/reference.xml <?xml version="1.0" encoding="iso-8859-1"?> <!-- $Revision: 1.1 $ --> <reference id="ref.simplexml"> <title>SimpleXML functions</title> <titleabbrev>SimpleXML</titleabbrev> <partintro> <section id="simplexml.intro"> &reftitle.intro; &warn.experimental; <para> The SimpleXML extension provides a very simple and easily usable toolset to convert XML to an object that can be processed with normal property selectors and array iterators. </para> </section> <section id="simplexml.installation"> &reftitle.install; <para> This extension is only available if PHP was configured with <option role="configure">--enable-simplexml</option>. The PHP configuration script does this by default. </para> </section> <section id="simplexml.examples"> &reftitle.examples; <para> Many examples in this reference require an XML string. Instead of repeating this string in every example, we put it into a file which we include in each example. This included file is shown in the following example section. Alternatively, you could create an XML document and read it with <function>simplexml_load_file</function>. </para> <para> <example> <title>Include file example.php with XML string</title> <programlisting role="php"> <![CDATA[ <?php $xmlstr = <<<XML <?xml version='1.0' standalone='yes'?> <movies> <movie> <title>PHP: Behind the Parser</title> <characters> <character> <name>Ms. Coder</name> <actor>Onlivia Actora</actor> </character> <character> <name>Mr. Coder</name> <actor>El Act&oacute;r</actor> </character> </characters> <plot> So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. </plot> <rating type="thumbs">7</rating> <rating type="stars">5</rating> </movie> </movies> XML; ?> ]]> </programlisting> </example> </para> <para> The simplicity of SimpleXML appears most clearly when one extracts a string or number from a basic XML document. <example> <title>Getting <literal>&lt;plot&gt;</literal></title> <programlisting role="php"><![CDATA[ <?php include 'example.php'; $xml = simplexml_load_string($xmlstr); echo $xml->movie[0]->plot; // "So this language. It's like..." ?> ]]> </programlisting> </example> </para> <para> <example> <title>Accessing non-unique elements in SimpleXML</title> <simpara> When multiple instances of an element exist as children of a single parent element, normal iteration techniques apply. </simpara> <programlisting role="php"><![CDATA[ <?php include 'example.php'; $xml = simplexml_load_string($xmlstr); /* For each <movie> node, we echo a separate <plot>. * [foreach() could be used, but for() stands in place * as using foreach() on large XML documents will * create undesirable memory overhead.] */ $mid_count = count($movies); for($mid = 0; $mid < $mid_count; ++$mid) { echo $movies[$mid]->plot,'<br/>'; } ?> ]]> </programlisting> </example> </para> <para> <example> <title>Using attributes</title> <simpara> So far, we have only covered the work of reading element names and their values. SimpleXML can also access element attributes. Access attributes of an element just as you would elements of an <type>array</type>. </simpara> <programlisting role="php"> <![CDATA[ <?php include 'example.php'; $xml = simplexml_load_string($xmlstr); /* Access the <rating> nodes of the first movie. * Output the rating scale, too. */ foreach($xml->movie[0]->rating as $rating) { switch($rating['type']) { // Get attributes as element indices case 'thumbs': echo $rating, ' thumbs up'; break; case 'stars': echo $rating, ' stars'; break; } } ?> ]]> </programlisting> </example> </para> <para> <example> <title>Using XPATH</title> <simpara> SimpleXML includes builtin <acronym>XPATH</acronym> support. To find all <literal>&lt;character&gt;</literal> elements: </simpara> <programlisting role="php"> <![CDATA[ <?php include 'example.php'; $xml = simplexml_load_string($xmlstr); foreach($xml->xpath('//character') as $character) { echo $character->name, 'played by ', $character->actor, '<br/>'; } ?> ]]> </programlisting> <simpara> '<literal>//</literal>' serves as a wildcard. To specify absolute paths, omit one of the slashes. </simpara> </example> </para> <para> <example> <title>Setting values</title> <simpara> Data in SimpleXML doesn't have to be constant. The object allows for manipulation of all of its elements. </simpara> <programlisting role="php"> <![CDATA[ <?php include 'example.php'; $xml = simplexml_load_string($xmlstr); $xml->movie[0]->actor[0]->age = '21'; echo $xml->asXML(); ?> ]]> </programlisting> <simpara> The above code will output a new XML document, just like the original, except that the new XML will define Ms. Coder's age as 21. </simpara> </example> </para> </section> </partintro> &reference.simplexml.functions; </reference> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/co.php/phpdoc/en/reference/simplexml/functions/simplexml-element-asXML.xml?r=1.1&p=1 Index: phpdoc/en/reference/simplexml/functions/simplexml-element-asXML.xml +++ phpdoc/en/reference/simplexml/functions/simplexml-element-asXML.xml <?xml version="1.0" encoding="iso-8859-1"?> <!-- $Revision: 1.1 $ --> <refentry id='function.simplexml-element-asXML'> <refnamediv> <refname>simplexml_element->asXML</refname> <refpurpose> Return a well-formed XML string based on SimpleXML element. </refpurpose> </refnamediv> <refsect1> <title>Description</title> <methodsynopsis> <type>string</type><methodname>simplexml_element->asXML</methodname> <void/> </methodsynopsis> <para> The <literal>asXML</literal> method formats the parent object's data in XML version 1.0. </para> <para> <example> <title>Get XML</title> <programlisting role="php"> <![CDATA[ <?php $string = <<<XML <a> <b> <c>text</c> <c>stuff</c> </b> <d> <c>code</c> </d> </a> XML; $xml = simplexml_load_string($string); echo $xml->asXML(); // <?xml ... <a><b><c>text</c><c>stuff</c> ... ?> ]]> </programlisting> </example> </para> <para> <literal>asXML</literal> also works on XPATH results: <example> <title> Using asXML() on <link linkend="function.simplexml-element-xpath">XPATH</link> results </title> <programlisting role="php"> <![CDATA[ <?php // Continued from example XML above. /* Search for <a><b><c> */ $result = $xml->xpath('/a/b/c'); while(list( , $node) = each($result)) { echo $node->asXML(); // <c>text</c> and <c>stuff</c> } ?> ]]> </programlisting> </example> </para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/co.php/phpdoc/en/reference/simplexml/functions/simplexml-element-attributes.xml?r=1.1&p=1 Index: phpdoc/en/reference/simplexml/functions/simplexml-element-attributes.xml +++ phpdoc/en/reference/simplexml/functions/simplexml-element-attributes.xml <?xml version="1.0" encoding="iso-8859-1"?> <!-- $Revision: 1.1 $ --> <refentry id='function.simplexml-element-attributes'> <refnamediv> <refname>simplexml_element->attributes</refname> <refpurpose> Identifies an element's attributes. </refpurpose> </refnamediv> <refsect1> <title>Description</title> <methodsynopsis> <type>object simplexml_element</type><methodname>simplexml_element->attributes</methodname> <methodparam><type>string</type><parameter>data</parameter></methodparam> </methodsynopsis> <para> This function provides the attributes and values defined within an xml tag. </para> <para> <example> <title>Interpret an XML string</title> <programlisting role="php"> <![CDATA[ <?php $string = <<<XML <a> <foo name="one" game="lonely">1</foo> </a> XML; $xml = simplexml_load_string($string); foreach($xml->foo[0]->attributes() as $a => $b) { echo $a,'="',$b,"\"\n"; } ?> ]]> </programlisting> <para> This script will display: </para> <screen> <![CDATA[ name="one" game="lonely" ]]> </screen> </example> </para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/co.php/phpdoc/en/reference/simplexml/functions/simplexml-element-children.xml?r=1.1&p=1 Index: phpdoc/en/reference/simplexml/functions/simplexml-element-children.xml +++ phpdoc/en/reference/simplexml/functions/simplexml-element-children.xml <?xml version="1.0" encoding="iso-8859-1"?> <!-- $Revision: 1.1 $ --> <refentry id='function.simplexml-element-children'> <refnamediv> <refname>simplexml_element->children</refname> <refpurpose> Finds children of given node. </refpurpose> </refnamediv> <refsect1> <title>Description</title> <methodsynopsis> <type>object simplexml_element</type><methodname>simplexml_element->children</methodname> <void/> </methodsynopsis> <para> This function finds the children of the element of which it is a member. The result follows normal iteration rules. </para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/co.php/phpdoc/en/reference/simplexml/functions/simplexml-element-xpath.xml?r=1.1&p=1 Index: phpdoc/en/reference/simplexml/functions/simplexml-element-xpath.xml +++ phpdoc/en/reference/simplexml/functions/simplexml-element-xpath.xml <?xml version="1.0" encoding="iso-8859-1"?> <!-- $Revision: 1.1 $ --> <refentry id='function.simplexml-element-xpath'> <refnamediv> <refname>simplexml_element->xpath</refname> <refpurpose> Runs XPATH query on XML data. </refpurpose> </refnamediv> <refsect1> <title>Description</title> <methodsynopsis> <type>array</type><methodname>simplexml_element->xpath</methodname> <methodparam><type>string</type><parameter>path</parameter></methodparam> </methodsynopsis> <para> The <literal>xpath</literal> method searches the SimpleXML node for children matching the <acronym>XPATH</acronym> <parameter>path</parameter>. It always returns an <type>array</type> of simplexml_element objects. </para> <para> <example> <title>XPATH</title> <programlisting role="php"> <![CDATA[ <?php $string = <<<XML <a> <b> <c>text</c> <c>stuff</c> </b> <d> <c>code</c> </d> </a> XML; $xml = simplexml_load_string($string); /* Search for <a><b><c> */ $result = $xml->xpath('/a/b/c'); while(list( , $node) = each($result)) { echo '/a/b/c: ',$node,"\n"; } /* Relative paths also work... */ $result = $xml->xpath('b/c'); while(list( , $node) = each($result)) { echo 'b/c: ',$node,"\n"; } ?> ]]> </programlisting> <para> This script will display: </para> <screen> <![CDATA[ /a/b/c: text /a/b/c: stuff b/c: text b/c: stuff ]]> </screen> <simpara> Notice that the two results are equal. </simpara> </example> </para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/co.php/phpdoc/en/reference/simplexml/functions/simplexml-load-dom.xml?r=1.1&p=1 Index: phpdoc/en/reference/simplexml/functions/simplexml-load-dom.xml +++ phpdoc/en/reference/simplexml/functions/simplexml-load-dom.xml <?xml version="1.0" encoding="iso-8859-1"?> <!-- $Revision: 1.1 $ --> <refentry id='function.simplexml-load-dom'> <refnamediv> <refname>simplexml_load_dom</refname> <refpurpose> <!-- ref.dom*xml* should really be ref.dom, but that's not written yet, so linking to ref.domxml (kennyt) --> Get a <literal>simplexml_element</literal> object from a DOM node. </refpurpose> </refnamediv> <refsect1> <title>Description</title> <methodsynopsis> <type>object simplexml_element</type><methodname>simplexml_load_dom</methodname> <methodparam><type>domNode</type><parameter>node</parameter></methodparam> </methodsynopsis> <para> <!-- same with this link --> This function takes a node of a <link linkend="ref.domxml">DOM</link> document and makes it into a SimpleXML node. This new object can then be used as a native SimpleXML element. If any errors occur, it returns &false;. </para> <!-- php5 DOM isn't documented; this depends on it... :| --> &warn.undocumented.func; </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/co.php/phpdoc/en/reference/simplexml/functions/simplexml-load-file.xml?r=1.1&p=1 Index: phpdoc/en/reference/simplexml/functions/simplexml-load-file.xml +++ phpdoc/en/reference/simplexml/functions/simplexml-load-file.xml <?xml version="1.0" encoding="iso-8859-1"?> <!-- $Revision: 1.1 $ --> <refentry id='function.simplexml-load-file'> <refnamediv> <refname>simplexml_load_file</refname> <refpurpose> Interprets an XML file into an object. </refpurpose> </refnamediv> <refsect1> <title>Description</title> <methodsynopsis> <type>object simplexml_element</type><methodname>simplexml_load_string</methodname> <methodparam><type>string</type><parameter>filename</parameter></methodparam> </methodsynopsis> <para> This function will convert the well-formed XML document in the file specified by <parameter>filename</parameter> to an <type>object</type> of class <literal>simplexml_element</literal>. If any errors occur during file access or interpretation, the function returns &false;. </para> <para> <example> <title>Interpret an XML document</title> <programlisting role="php"> <![CDATA[ <?php // The file test.xml contains an XML document with a root element // and at least an element /[root]/title. if (file_exists('test.xml')) { $xml = simplexml_load_file('test.xml'); var_dump($xml); } else { exit('Failed to open test.xml.'); } ?> ]]> </programlisting> <para> This script will display, on success: </para> <screen> <![CDATA[ simplexml_element Object ( [title] => Example Title ... ) ]]> </screen> <simpara> At this point, you can go about using <literal>$xml->title</literal> and any other elements. </simpara> </example> </para> <para> See also: <function>simplexml_load_string</function> </para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/co.php/phpdoc/en/reference/simplexml/functions/simplexml-load-string.xml?r=1.1&p=1 Index: phpdoc/en/reference/simplexml/functions/simplexml-load-string.xml +++ phpdoc/en/reference/simplexml/functions/simplexml-load-string.xml <?xml version="1.0" encoding="iso-8859-1"?> <!-- $Revision: 1.1 $ --> <refentry id='function.simplexml-load-string'> <refnamediv> <refname>simplexml_load_string</refname> <refpurpose> Interprets a string of XML into an object. </refpurpose> </refnamediv> <refsect1> <title>Description</title> <methodsynopsis> <type>object simplexml_element</type><methodname>simplexml_load_string</methodname> <methodparam><type>string</type><parameter>data</parameter></methodparam> </methodsynopsis> <para> This function will take the well-formed xml string <parameter>data</parameter> and return an object with properties containing the data held within the xml document. If any errors occur, it returns &false;. </para> <para> <example> <title>Interpret an XML string</title> <programlisting role="php"> <![CDATA[ <?php $string = <<<XML <?xml version='1.0'?> <document> <title>Forty What?</title> <from>Joe</from> <to>Jane</to> <body> I know that's the answer -- but what's the question? </body> </document> XML; $xml = simplexml_load_string($string) var_dump($xml); ?> ]]> </programlisting> <para> This script will display: </para> <screen> <![CDATA[ simplexml_element Object ( [title] => Forty What? [from] => Joe [to] => Jane [body] => I know that's the answer -- but what's the question? ) ]]> </screen> <simpara> At this point, you can go about using <literal>$xml->body</literal> and such. </simpara> </example> </para> <para> See also: <function>simplexml_load_file</function>. </para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->
« previous php.doc (#969358420) next »