mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
Added new SimpleXML reference.
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@149543 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
c2e8152c2a
commit
48bedcaea0
8 changed files with 753 additions and 0 deletions
93
reference/simplexml/functions/simplexml-element-asXML.xml
Normal file
93
reference/simplexml/functions/simplexml-element-asXML.xml
Normal file
|
@ -0,0 +1,93 @@
|
|||
<?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
|
||||
-->
|
|
@ -0,0 +1,71 @@
|
|||
<?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
|
||||
-->
|
42
reference/simplexml/functions/simplexml-element-children.xml
Normal file
42
reference/simplexml/functions/simplexml-element-children.xml
Normal file
|
@ -0,0 +1,42 @@
|
|||
<?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
|
||||
-->
|
95
reference/simplexml/functions/simplexml-element-xpath.xml
Normal file
95
reference/simplexml/functions/simplexml-element-xpath.xml
Normal file
|
@ -0,0 +1,95 @@
|
|||
<?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
|
||||
-->
|
50
reference/simplexml/functions/simplexml-load-dom.xml
Normal file
50
reference/simplexml/functions/simplexml-load-dom.xml
Normal file
|
@ -0,0 +1,50 @@
|
|||
<?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
|
||||
-->
|
84
reference/simplexml/functions/simplexml-load-file.xml
Normal file
84
reference/simplexml/functions/simplexml-load-file.xml
Normal file
|
@ -0,0 +1,84 @@
|
|||
<?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
|
||||
-->
|
92
reference/simplexml/functions/simplexml-load-string.xml
Normal file
92
reference/simplexml/functions/simplexml-load-string.xml
Normal file
|
@ -0,0 +1,92 @@
|
|||
<?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
|
||||
-->
|
226
reference/simplexml/reference.xml
Normal file
226
reference/simplexml/reference.xml
Normal file
|
@ -0,0 +1,226 @@
|
|||
<?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ó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><plot></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><character></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
|
||||
-->
|
||||
|
Loading…
Reference in a new issue