Source of: /manual/en/simplexml.examples-basic.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/simplexml.examples.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'simplexml.examples-basic.php',
1 => 'Basic usage',
),
'up' =>
array (
0 => 'simplexml.examples.php',
1 => 'Examples',
),
'prev' =>
array (
0 => 'simplexml.examples.php',
1 => 'Examples',
),
'next' =>
array (
0 => 'simplexml.examples-errors.php',
1 => 'Dealing with XML errors',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="simplexml.examples-basic" class="section">
<h2 class="title">Basic usage</h2>
<p class="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 <a href="function.simplexml-load-file.php" class="function">simplexml_load_file()</a>.
</p>
<p class="para">
</p><div class="example">
<p><b>Example #1 Include file example.php with XML string</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br />$xmlstr </span><span style="color: #007700">= <<<XML<br /></span><span style="color: #DD0000"><?xml version='1.0' standalone='yes'?><br /><movies><br /> <movie><br /> <title>PHP: Behind the Parser</title><br /> <characters><br /> <character><br /> <name>Ms. Coder</name><br /> <actor>Onlivia Actora</actor><br /> </character><br /> <character><br /> <name>Mr. Coder</name><br /> <actor>El Act&#211;r</actor><br /> </character><br /> </characters><br /> <plot><br /> So, this language. It's like, a programming language. Or is it a<br /> scripting language? All is revealed in this thrilling horror spoof<br /> of a documentary.<br /> </plot><br /> <great-lines><br /> <line>PHP solves all my web problems</line><br /> </great-lines><br /> <rating type="thumbs">7</rating><br /> <rating type="stars">5</rating><br /> </movie><br /></movies><br /></span><span style="color: #007700">XML;<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
<p class="para">
The simplicity of SimpleXML appears most clearly when one extracts
a string or number from a basic XML document.
</p><div class="example">
<p><b>Example #2 Getting <i><plot></i></b></p>
<div class="example-contents programlisting"><div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$xml </span><span style="color: #007700">= new </span><span style="color: #0000BB">SimpleXMLElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$xmlstr</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #0000BB">$xml</span><span style="color: #007700">-></span><span style="color: #0000BB">movie</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-></span><span style="color: #0000BB">plot</span><span style="color: #007700">; </span><span style="color: #FF8000">// "So this language. It's like..."<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
<p class="para">
Accessing elements within an XML document that contain characters not permitted under
PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the
element name within braces and the apostrophe.
</p><div class="example">
<p><b>Example #3 Getting <i><line></i></b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$xml </span><span style="color: #007700">= new </span><span style="color: #0000BB">SimpleXMLElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$xmlstr</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #0000BB">$xml</span><span style="color: #007700">-></span><span style="color: #0000BB">movie</span><span style="color: #007700">->{</span><span style="color: #DD0000">'great-lines'</span><span style="color: #007700">}-></span><span style="color: #0000BB">line</span><span style="color: #007700">; </span><span style="color: #FF8000">// "PHP solves all my web problems"<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
<p class="para">
</p><div class="example">
<p><b>Example #4 Accessing non-unique elements in SimpleXML</b></p>
<div class="example-contents simpara"><p>
When multiple instances of an element exist as children of
a single parent element, normal iteration techniques apply.
</p></div>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$xml </span><span style="color: #007700">= new </span><span style="color: #0000BB">SimpleXMLElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$xmlstr</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* For each <movie> node, we echo a separate <plot>. */<br /></span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-></span><span style="color: #0000BB">movie </span><span style="color: #007700">as </span><span style="color: #0000BB">$movie</span><span style="color: #007700">) {<br /> echo </span><span style="color: #0000BB">$movie</span><span style="color: #007700">-></span><span style="color: #0000BB">plot</span><span style="color: #007700">, </span><span style="color: #DD0000">'<br />'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
<p class="para">
</p><div class="example">
<p><b>Example #5 Using attributes</b></p>
<div class="example-contents simpara"><p>
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 <a href="language.types.array.php" class="type array">array</a>.
</p></div>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$xml </span><span style="color: #007700">= new </span><span style="color: #0000BB">SimpleXMLElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$xmlstr</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Access the <rating> nodes of the first movie.<br /> * Output the rating scale, too. */<br /></span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-></span><span style="color: #0000BB">movie</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-></span><span style="color: #0000BB">rating </span><span style="color: #007700">as </span><span style="color: #0000BB">$rating</span><span style="color: #007700">) {<br /> switch((string) </span><span style="color: #0000BB">$rating</span><span style="color: #007700">[</span><span style="color: #DD0000">'type'</span><span style="color: #007700">]) { </span><span style="color: #FF8000">// Get attributes as element indices<br /> </span><span style="color: #007700">case </span><span style="color: #DD0000">'thumbs'</span><span style="color: #007700">:<br /> echo </span><span style="color: #0000BB">$rating</span><span style="color: #007700">, </span><span style="color: #DD0000">' thumbs up'</span><span style="color: #007700">;<br /> break;<br /> case </span><span style="color: #DD0000">'stars'</span><span style="color: #007700">:<br /> echo </span><span style="color: #0000BB">$rating</span><span style="color: #007700">, </span><span style="color: #DD0000">' stars'</span><span style="color: #007700">;<br /> break;<br /> }<br />}<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
<p class="para">
</p><div class="example">
<p><b>Example #6 Comparing Elements and Attributes with Text</b></p>
<div class="example-contents simpara"><p>
To compare an element or attribute with a string or pass it into a
function that requires a string, you must cast it to a string using
<i>(string)</i>. Otherwise, PHP treats the element as an object.
</p></div>
<div class="example-contents programlisting"><div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php <br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$xml </span><span style="color: #007700">= new </span><span style="color: #0000BB">SimpleXMLElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$xmlstr</span><span style="color: #007700">);<br /><br />if ((string) </span><span style="color: #0000BB">$xml</span><span style="color: #007700">-></span><span style="color: #0000BB">movie</span><span style="color: #007700">-></span><span style="color: #0000BB">title </span><span style="color: #007700">== </span><span style="color: #DD0000">'PHP: Behind the Parser'</span><span style="color: #007700">) {<br /> print </span><span style="color: #DD0000">'My favorite movie.'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">((string) </span><span style="color: #0000BB">$xml</span><span style="color: #007700">-></span><span style="color: #0000BB">movie</span><span style="color: #007700">-></span><span style="color: #0000BB">title</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
<p class="para">
</p><div class="example">
<p><b>Example #7 Comparing Two Elements</b></p>
<div class="example-contents simpara"><p>
Two SimpleXMLElements are considered different even if they point to the
same element since PHP 5.2.0.
</p></div>
<div class="example-contents programlisting"><div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br />$el1 </span><span style="color: #007700">= new </span><span style="color: #0000BB">SimpleXMLElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$xmlstr</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$el2 </span><span style="color: #007700">= new </span><span style="color: #0000BB">SimpleXMLElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$xmlstr</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$el1 </span><span style="color: #007700">== </span><span style="color: #0000BB">$el2</span><span style="color: #007700">); </span><span style="color: #FF8000">// false since PHP 5.2.0<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
<p class="para">
</p><div class="example">
<p><b>Example #8 Using XPath</b></p>
<div class="example-contents simpara"><p>
SimpleXML includes built-in <acronym title="XML Path Language">XPath</acronym> support.
To find all <i><character></i> elements:
</p></div>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$xml </span><span style="color: #007700">= new </span><span style="color: #0000BB">SimpleXMLElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$xmlstr</span><span style="color: #007700">);<br /><br />foreach (</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-></span><span style="color: #0000BB">xpath</span><span style="color: #007700">(</span><span style="color: #DD0000">'//character'</span><span style="color: #007700">) as </span><span style="color: #0000BB">$character</span><span style="color: #007700">) {<br /> echo </span><span style="color: #0000BB">$character</span><span style="color: #007700">-></span><span style="color: #0000BB">name</span><span style="color: #007700">, </span><span style="color: #DD0000">'played by '</span><span style="color: #007700">, </span><span style="color: #0000BB">$character</span><span style="color: #007700">-></span><span style="color: #0000BB">actor</span><span style="color: #007700">, </span><span style="color: #DD0000">'<br />'</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents simpara"><p>
'<i>//</i>' serves as a wildcard. To specify absolute
paths, omit one of the slashes.
</p></div>
</div><p>
</p>
<p class="para">
</p><div class="example">
<p><b>Example #9 Setting values</b></p>
<div class="example-contents simpara"><p>
Data in SimpleXML doesn't have to be constant. The object allows
for manipulation of all of its elements.
</p></div>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$xml </span><span style="color: #007700">= new </span><span style="color: #0000BB">SimpleXMLElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$xmlstr</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$xml</span><span style="color: #007700">-></span><span style="color: #0000BB">movie</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-></span><span style="color: #0000BB">characters</span><span style="color: #007700">-></span><span style="color: #0000BB">character</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-></span><span style="color: #0000BB">name </span><span style="color: #007700">= </span><span style="color: #DD0000">'Miss Coder'</span><span style="color: #007700">;<br /><br />echo </span><span style="color: #0000BB">$xml</span><span style="color: #007700">-></span><span style="color: #0000BB">asXML</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents simpara"><p>
The above code will output a new XML document, just like the original,
except that the new XML will change Ms. Coder to Miss Coder.
</p></div>
</div><p>
</p>
<p class="para">
</p><div class="example">
<p><b>Example #10 Adding elements and attributes</b></p>
<div class="example-contents simpara"><p>
Since PHP 5.1.3, SimpleXML has had the ability to easily add children and
attributes.
</p></div>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">include </span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$xml </span><span style="color: #007700">= new </span><span style="color: #0000BB">SimpleXMLElement</span><span style="color: #007700">(</span><span style="color: #0000BB">$xmlstr</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$character </span><span style="color: #007700">= </span><span style="color: #0000BB">$xml</span><span style="color: #007700">-></span><span style="color: #0000BB">movie</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-></span><span style="color: #0000BB">characters</span><span style="color: #007700">-></span><span style="color: #0000BB">addChild</span><span style="color: #007700">(</span><span style="color: #DD0000">'character'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$character</span><span style="color: #007700">-></span><span style="color: #0000BB">addChild</span><span style="color: #007700">(</span><span style="color: #DD0000">'name'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Mr. Parser'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$character</span><span style="color: #007700">-></span><span style="color: #0000BB">addChild</span><span style="color: #007700">(</span><span style="color: #DD0000">'actor'</span><span style="color: #007700">, </span><span style="color: #DD0000">'John Doe'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$rating </span><span style="color: #007700">= </span><span style="color: #0000BB">$xml</span><span style="color: #007700">-></span><span style="color: #0000BB">movie</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-></span><span style="color: #0000BB">addChild</span><span style="color: #007700">(</span><span style="color: #DD0000">'rating'</span><span style="color: #007700">, </span><span style="color: #DD0000">'PG'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$rating</span><span style="color: #007700">-></span><span style="color: #0000BB">addAttribute</span><span style="color: #007700">(</span><span style="color: #DD0000">'type'</span><span style="color: #007700">, </span><span style="color: #DD0000">'mpaa'</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #0000BB">$xml</span><span style="color: #007700">-></span><span style="color: #0000BB">asXML</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents simpara"><p>
The above code will output an XML document based on the original but
having a new character and rating.
</p></div>
</div><p>
</p>
<p class="para">
</p><div class="example">
<p><b>Example #11 DOM Interoperability</b></p>
<div class="example-contents simpara"><p>
PHP has a mechanism to convert XML nodes between SimpleXML
and DOM formats. This example shows how one might change
a DOM element to SimpleXML.
</p></div>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br />$dom </span><span style="color: #007700">= new </span><span style="color: #0000BB">domDocument</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$dom</span><span style="color: #007700">-></span><span style="color: #0000BB">loadXML</span><span style="color: #007700">(</span><span style="color: #DD0000">'<books><book><title>blah</title></book></books>'</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$dom</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">'Error while parsing the document'</span><span style="color: #007700">;<br /> exit;<br />}<br /><br /></span><span style="color: #0000BB">$s </span><span style="color: #007700">= </span><span style="color: #0000BB">simplexml_import_dom</span><span style="color: #007700">(</span><span style="color: #0000BB">$dom</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #0000BB">$s</span><span style="color: #007700">-></span><span style="color: #0000BB">book</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-></span><span style="color: #0000BB">title</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
</div><?php manual_footer(); ?>