Added __construct() page for SimpleXML and modified examples to show usage of SimpleXML with SimpleXMLElement for a more OO approach.

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@214069 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Ben Ramsey 2006-05-31 22:54:02 +00:00
parent efc77ab610
commit b48693bee3
13 changed files with 294 additions and 80 deletions

View file

@ -1,20 +1,23 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.4 $ -->
<!-- $Revision: 1.5 $ -->
<refentry id='function.simplexml-element-addAttribute'>
<refnamediv>
<refname>SimpleXMLElement->addAttribute</refname>
<refname>SimpleXMLElement->addAttribute()</refname>
<refpurpose>
Adds an attribute to the SimpleXML element
</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>void</type><methodname>SimpleXMLElement->addAttribute</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
<methodparam><type>string</type><parameter>value</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>namespace</parameter></methodparam>
</methodsynopsis>
<classsynopsis>
<ooclass><classname>SimpleXMLElement</classname></ooclass>
<methodsynopsis>
<type>void</type><methodname>addAttribute</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
<methodparam><type>string</type><parameter>value</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>namespace</parameter></methodparam>
</methodsynopsis>
</classsynopsis>
<para>
Adds an attribute to the SimpleXML element.
</para>
@ -60,8 +63,10 @@
<programlisting role="php">
<![CDATA[
<?php
include 'example.php';
$sxe = new SimpleXMLElement($xmlstr); // or use simplexml_load_string()
$sxe = new SimpleXMLElement($xmlstr);
$sxe->addAttribute('type', 'documentary');
$movie = $sxe->addChild('movie');

View file

@ -1,20 +1,23 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.4 $ -->
<!-- $Revision: 1.5 $ -->
<refentry id='function.simplexml-element-addChild'>
<refnamediv>
<refname>SimpleXMLElement->addChild</refname>
<refname>SimpleXMLElement->addChild()</refname>
<refpurpose>
Adds a child element to the XML node
</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>SimpleXMLElement</type><methodname>SimpleXMLElement->addChild</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>value</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>namespace</parameter></methodparam>
</methodsynopsis>
<classsynopsis>
<ooclass><classname>SimpleXMLElement</classname></ooclass>
<methodsynopsis>
<type>SimpleXMLElement</type><methodname>addChild</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>value</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>namespace</parameter></methodparam>
</methodsynopsis>
</classsynopsis>
<para>
Adds a child element to the node and returns a SimpleXMLElement of the child.
</para>
@ -68,8 +71,10 @@
<programlisting role="php">
<![CDATA[
<?php
include 'example.php';
$sxe = new SimpleXMLElement($xmlstr); // or use simplexml_load_string()
$sxe = new SimpleXMLElement($xmlstr);
$sxe->addAttribute('type', 'documentary');
$movie = $sxe->addChild('movie');

View file

@ -1,18 +1,21 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.5 $ -->
<!-- $Revision: 1.6 $ -->
<refentry id='function.simplexml-element-asXML'>
<refnamediv>
<refname>SimpleXMLElement->asXML</refname>
<refname>SimpleXMLElement->asXML()</refname>
<refpurpose>
Return a well-formed XML string based on SimpleXML element
</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>mixed</type><methodname>SimpleXMLElement->asXML</methodname>
<methodparam choice="opt"><type>string</type><parameter>filename</parameter></methodparam>
</methodsynopsis>
<classsynopsis>
<ooclass><classname>SimpleXMLElement</classname></ooclass>
<methodsynopsis>
<type>mixed</type><methodname>asXML</methodname>
<methodparam choice="opt"><type>string</type><parameter>filename</parameter></methodparam>
</methodsynopsis>
</classsynopsis>
<para>
The <literal>asXML</literal> method formats the parent object's data
in XML version 1.0.
@ -66,7 +69,7 @@ $string = <<<XML
</a>
XML;
$xml = simplexml_load_string($string);
$xml = new SimpleXMLElement($string);
echo $xml->asXML(); // <?xml ... <a><b><c>text</c><c>stuff</c> ...

View file

@ -1,18 +1,22 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.6 $ -->
<!-- $Revision: 1.7 $ -->
<refentry id='function.simplexml-element-attributes'>
<refnamediv>
<refname>SimpleXMLElement->attributes</refname>
<refname>SimpleXMLElement->attributes()</refname>
<refpurpose>
Identifies an element's attributes
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>SimpleXMLElement</type><methodname>simplexml_element->attributes</methodname>
<methodparam choice="opt"><type>string</type><parameter>data</parameter></methodparam>
</methodsynopsis>
<classsynopsis>
<ooclass><classname>SimpleXMLElement</classname></ooclass>
<methodsynopsis>
<type>SimpleXMLElement</type><methodname>attributes</methodname>
<methodparam choice="opt"><type>string</type><parameter>ns</parameter></methodparam>
<methodparam choice="opt"><type>bool</type><parameter>is_prefix</parameter></methodparam>
</methodsynopsis>
</classsynopsis>
<para>
This function provides the attributes and values defined within an xml tag.
</para>

View file

@ -1,18 +1,22 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.8 $ -->
<!-- $Revision: 1.9 $ -->
<refentry id='function.simplexml-element-children'>
<refnamediv>
<refname>SimpleXMLElement->children</refname>
<refname>SimpleXMLElement->children()</refname>
<refpurpose>
Finds children of given node
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>SimpleXMLElement</type><methodname>simplexml_element->children</methodname>
<methodparam choice="opt"><type>string</type><parameter>nsprefix</parameter></methodparam>
</methodsynopsis>
<classsynopsis>
<ooclass><classname>SimpleXMLElement</classname></ooclass>
<methodsynopsis>
<type>SimpleXMLElement</type><methodname>children</methodname>
<methodparam choice="opt"><type>string</type><parameter>ns</parameter></methodparam>
<methodparam choice="opt"><type>bool</type><parameter>is_prefix</parameter></methodparam>
</methodsynopsis>
</classsynopsis>
<para>
This method finds the children of the element of which it is a member. The result
follows normal iteration rules.
@ -23,7 +27,7 @@
<programlisting role="php">
<![CDATA[
<?php
$xml = simplexml_load_string(
$xml = new SimpleXMLElement(
'<person>
<child role="son">
<child role="daughter"/>

View file

@ -0,0 +1,137 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.1 $ -->
<refentry id='function.simplexml-element-construct'>
<refnamediv>
<refname>SimpleXMLElement->__construct()</refname>
<refpurpose>
Creates a new SimpleXMLElement object
</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<classsynopsis>
<ooclass><classname>SimpleXMLElement</classname></ooclass>
<constructorsynopsis>
<methodname>__construct</methodname>
<methodparam><type>string</type><parameter>data</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>options</parameter></methodparam>
<methodparam choice="opt"><type>bool</type><parameter>data_is_url</parameter></methodparam>
<methodparam choice="opt"><type>string</type><parameter>ns</parameter></methodparam>
<methodparam choice="opt"><type>bool</type><parameter>is_prefix</parameter></methodparam>
</constructorsynopsis>
</classsynopsis>
<para>
Creates a new <classname>SimpleXMLElement</classname> object
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>data</parameter></term>
<listitem>
<para>
A well-formed XML string or the path or URL to an XML document if
<parameter>data_is_url</parameter> is &true;.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>options</parameter></term>
<listitem>
<para>
Optionally used to specify <link linkend="libxml.constants">additional
Libxml parameters</link>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>data_is_url</parameter></term>
<listitem>
<para>
By default, <parameter>data_is_url</parameter> is &false;. Use &true; to
specify that <parameter>data</parameter> is a path or URL to an XML
document instead of <type>string</type> data.
</para>
</listitem>
</varlistentry>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns a <type>SimpleXMLElement</type> object representing
<parameter>data</parameter>.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Create a SimpleXMLElement object</title>
<programlisting role="php">
<![CDATA[
<?php
include 'example.php';
$sxe = new SimpleXMLElement($xmlstr);
echo $sxe->movie[0]->title;
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>Create a SimpleXMLElement object from a URL</title>
<programlisting role="php">
<![CDATA[
<?php
$sxe = new SimpleXMLElement('http://example.org/document.xml', NULL, TRUE);
echo $sxe->asXML();
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><xref linkend="function.simplexml-load-string" /></member>
<member><xref linkend="function.simplexml-load-file" /></member>
</simplelist>
</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
-->

View file

@ -1,18 +1,21 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.5 $ -->
<!-- $Revision: 1.6 $ -->
<refentry id='function.simplexml-element-getDocNamespaces'>
<refnamediv>
<refname>SimpleXMLElement->getDocNamespaces</refname>
<refname>SimpleXMLElement->getDocNamespaces()</refname>
<refpurpose>
Returns namespaces declared in document
</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>array</type><methodname>SimpleXMLElement->getDocNamespaces</methodname>
<methodparam choice="opt"><type>bool</type><parameter>recursive</parameter></methodparam>
</methodsynopsis>
<classsynopsis>
<ooclass><classname>SimpleXMLElement</classname></ooclass>
<methodsynopsis>
<type>array</type><methodname>getDocNamespaces</methodname>
<methodparam choice="opt"><type>bool</type><parameter>recursive</parameter></methodparam>
</methodsynopsis>
</classsynopsis>
<para>
Returns namespaces declared in document
</para>
@ -60,7 +63,7 @@ $xml = <<<XML
</people>
XML;
$sxe = new SimpleXMLElement($xml); // or use simplexml_load_string()
$sxe = new SimpleXMLElement($xml);
$namespaces = $sxe->getDocNamespaces();
var_dump($namespaces);
@ -87,7 +90,7 @@ $xml = <<<XML
</people>
XML;
$sxe = new SimpleXMLElement($xml); // or use simplexml_load_string()
$sxe = new SimpleXMLElement($xml);
$namespaces = $sxe->getDocNamespaces(TRUE);
var_dump($namespaces);

View file

@ -1,16 +1,19 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.4 $ -->
<!-- $Revision: 1.5 $ -->
<refentry id='function.simplexml-element-getName'>
<refnamediv>
<refname>SimpleXMLElement->getName</refname>
<refname>SimpleXMLElement->getName()</refname>
<refpurpose>Gets the name of the XML element</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>string</type><methodname>SimpleXMLElement->getName</methodname>
<void/>
</methodsynopsis>
<classsynopsis>
<ooclass><classname>SimpleXMLElement</classname></ooclass>
<methodsynopsis>
<type>string</type><methodname>getName</methodname>
<void/>
</methodsynopsis>
</classsynopsis>
<para>
Gets the name of the XML element.
</para>
@ -33,7 +36,7 @@
<![CDATA[
<?php
$sxe = new SimpleXMLElement($xmlstr); // or use simplexml_load_string()
$sxe = new SimpleXMLElement($xmlstr);
echo $sxe->getName() . "\n";

View file

@ -1,18 +1,21 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.5 $ -->
<!-- $Revision: 1.6 $ -->
<refentry id='function.simplexml-element-getNamespaces'>
<refnamediv>
<refname>SimpleXMLElement->getNamespaces</refname>
<refname>SimpleXMLElement->getNamespaces()</refname>
<refpurpose>
Returns namespaces used in document
</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>array</type><methodname>SimpleXMLElement->getNamespaces</methodname>
<methodparam choice="opt"><type>bool</type><parameter>recursive</parameter></methodparam>
</methodsynopsis>
<classsynopsis>
<ooclass><classname>SimpleXMLElement</classname></ooclass>
<methodsynopsis>
<type>array</type><methodname>getNamespaces</methodname>
<methodparam choice="opt"><type>bool</type><parameter>recursive</parameter></methodparam>
</methodsynopsis>
</classsynopsis>
<para>
Returns namespaces used in document
</para>
@ -60,7 +63,7 @@ $xml = <<<XML
</people>
XML;
$sxe = new SimpleXMLElement($xml); // or use simplexml_load_string()
$sxe = new SimpleXMLElement($xml);
$namespaces = $sxe->getNamespaces(TRUE);
var_dump($namespaces);

View file

@ -1,21 +1,24 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.4 $ -->
<!-- $Revision: 1.5 $ -->
<refentry id='function.simplexml-element-xpath'>
<refnamediv>
<refname>SimpleXMLElement->xpath</refname>
<refname>SimpleXMLElement->xpath()</refname>
<refpurpose>
Runs Xpath query on XML data
Runs XPath query on XML data
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>array</type><methodname>SimpleXMLElement->xpath</methodname>
<methodparam><type>string</type><parameter>path</parameter></methodparam>
</methodsynopsis>
<classsynopsis>
<ooclass><classname>SimpleXMLElement</classname></ooclass>
<methodsynopsis>
<type>array</type><methodname>xpath</methodname>
<methodparam><type>string</type><parameter>path</parameter></methodparam>
</methodsynopsis>
</classsynopsis>
<para>
The <literal>xpath</literal> method searches the SimpleXML node for
children matching the <acronym>Xpath</acronym> <parameter>path</parameter>.
children matching the <acronym>XPath</acronym> <parameter>path</parameter>.
It always returns an <type>array</type> of SimpleXMLElement objects.
</para>
<para>
@ -36,7 +39,7 @@ $string = <<<XML
</a>
XML;
$xml = simplexml_load_string($string);
$xml = new SimpleXMLElement($string);
/* Search for <a><b><c> */
$result = $xml->xpath('/a/b/c');

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.11 $ -->
<!-- $Revision: 1.12 $ -->
<refentry id='function.simplexml-load-file'>
<refnamediv>
<refname>simplexml_load_file</refname>
@ -79,8 +79,15 @@ SimpleXMLElement Object
</simpara>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
See also: <function>simplexml_load_string</function>
<simplelist>
<member><xref linkend="function.simplexml-load-file" /></member>
<member><xref linkend="function.simplexml-element-construct" /></member>
</simplelist>
</para>
</refsect1>
</refentry>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.9 $ -->
<!-- $Revision: 1.10 $ -->
<refentry id='function.simplexml-load-string'>
<refnamediv>
<refname>simplexml_load_string</refname>
@ -77,8 +77,15 @@ SimpleXMLElement Object
</simpara>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
See also: <function>simplexml_load_file</function>.
<simplelist>
<member><xref linkend="function.simplexml-load-file" /></member>
<member><xref linkend="function.simplexml-element-construct" /></member>
</simplelist>
</para>
</refsect1>
</refentry>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.17 $ -->
<!-- $Revision: 1.18 $ -->
<!-- Purpose: xml -->
<!-- Membership: bundled, external -->
@ -80,7 +80,7 @@ XML;
<?php
include 'example.php';
$xml = simplexml_load_string($xmlstr);
$xml = new SimpleXMLElement($xmlstr);
echo $xml->movie[0]->plot; // "So this language. It's like..."
?>
@ -99,7 +99,7 @@ echo $xml->movie[0]->plot; // "So this language. It's like..."
<?php
include 'example.php';
$xml = simplexml_load_string($xmlstr);
$xml = new SimpleXMLElement($xmlstr);
/* For each <movie> node, we echo a separate <plot>. */
foreach ($xml->movie as $movie) {
@ -125,7 +125,7 @@ foreach ($xml->movie as $movie) {
<?php
include 'example.php';
$xml = simplexml_load_string($xmlstr);
$xml = new SimpleXMLElement($xmlstr);
/* Access the <rating> nodes of the first movie.
* Output the rating scale, too. */
@ -156,7 +156,7 @@ foreach ($xml->movie[0]->rating as $rating) {
<?php
include 'example.php';
$xml = simplexml_load_string($xmlstr);
$xml = new SimpleXMLElement($xmlstr);
if ((string) $xml->movie->title == 'PHP: Behind the Parser') {
print 'My favorite movie.';
@ -179,7 +179,7 @@ htmlentities((string) $xml->movie->title);
<![CDATA[
<?php
include 'example.php';
$xml = simplexml_load_string($xmlstr);
$xml = new SimpleXMLElement($xmlstr);
foreach ($xml->xpath('//character') as $character) {
echo $character->name, 'played by ', $character->actor, '<br />';
@ -204,7 +204,7 @@ foreach ($xml->xpath('//character') as $character) {
<![CDATA[
<?php
include 'example.php';
$xml = simplexml_load_string($xmlstr);
$xml = new SimpleXMLElement($xmlstr);
$xml->movie[0]->characters->character[0]->name = 'Miss Coder';
@ -218,6 +218,36 @@ echo $xml->asXML();
</simpara>
</example>
</para>
<para>
<example>
<title>Adding elements and attributes</title>
<simpara>
Since PHP 5.1.3, SimpleXML has had the ability to easily add children and
attributes.
</simpara>
<programlisting role="php">
<![CDATA[
<?php
include 'example.php';
$xml = new SimpleXMLElement($xmlstr);
$character = $xml->movie[0]->characters->addChild('character');
$character->addChild('name', 'Mr. Parser');
$character->addChild('actor', 'John Doe');
$rating = $xml->movie[0]->addChild('rating', 'PG');
$rating->addAttribute('type', 'mpaa');
echo $xml->asXML();
?>
]]>
</programlisting>
<simpara>
The above code will output an XML document based on the original but
having a new character and rating.
</simpara>
</example>
</para>
<para>
<example>
<title>DOM Interoperability</title>