downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

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">&lt;?php<br />$xmlstr&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;XML<br /></span><span style="color: #DD0000">&lt;?xml&nbsp;version='1.0'&nbsp;standalone='yes'?&gt;<br />&lt;movies&gt;<br />&nbsp;&lt;movie&gt;<br />&nbsp;&nbsp;&lt;title&gt;PHP:&nbsp;Behind&nbsp;the&nbsp;Parser&lt;/title&gt;<br />&nbsp;&nbsp;&lt;characters&gt;<br />&nbsp;&nbsp;&nbsp;&lt;character&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;name&gt;Ms.&nbsp;Coder&lt;/name&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;actor&gt;Onlivia&nbsp;Actora&lt;/actor&gt;<br />&nbsp;&nbsp;&nbsp;&lt;/character&gt;<br />&nbsp;&nbsp;&nbsp;&lt;character&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;name&gt;Mr.&nbsp;Coder&lt;/name&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;actor&gt;El&nbsp;Act&amp;#211;r&lt;/actor&gt;<br />&nbsp;&nbsp;&nbsp;&lt;/character&gt;<br />&nbsp;&nbsp;&lt;/characters&gt;<br />&nbsp;&nbsp;&lt;plot&gt;<br />&nbsp;&nbsp;&nbsp;So,&nbsp;this&nbsp;language.&nbsp;It's&nbsp;like,&nbsp;a&nbsp;programming&nbsp;language.&nbsp;Or&nbsp;is&nbsp;it&nbsp;a<br />&nbsp;&nbsp;&nbsp;scripting&nbsp;language?&nbsp;All&nbsp;is&nbsp;revealed&nbsp;in&nbsp;this&nbsp;thrilling&nbsp;horror&nbsp;spoof<br />&nbsp;&nbsp;&nbsp;of&nbsp;a&nbsp;documentary.<br />&nbsp;&nbsp;&lt;/plot&gt;<br />&nbsp;&nbsp;&lt;great-lines&gt;<br />&nbsp;&nbsp;&nbsp;&lt;line&gt;PHP&nbsp;solves&nbsp;all&nbsp;my&nbsp;web&nbsp;problems&lt;/line&gt;<br />&nbsp;&nbsp;&lt;/great-lines&gt;<br />&nbsp;&nbsp;&lt;rating&nbsp;type="thumbs"&gt;7&lt;/rating&gt;<br />&nbsp;&nbsp;&lt;rating&nbsp;type="stars"&gt;5&lt;/rating&gt;<br />&nbsp;&lt;/movie&gt;<br />&lt;/movies&gt;<br /></span><span style="color: #007700">XML;<br /></span><span style="color: #0000BB">?&gt;</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>&lt;plot&gt;</i></b></p>
    <div class="example-contents programlisting"><div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$xml&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</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&nbsp;</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">plot</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;"So&nbsp;this&nbsp;language.&nbsp;It's&nbsp;like..."<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div><p>
  </p>
  <p class="para">
   Accessing elements within an XML document that contain characters not permitted under
   PHP&#039;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>&lt;line&gt;</i></b></p>
    <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$xml&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</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&nbsp;</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">-&gt;{</span><span style="color: #DD0000">'great-lines'</span><span style="color: #007700">}-&gt;</span><span style="color: #0000BB">line</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;"PHP&nbsp;solves&nbsp;all&nbsp;my&nbsp;web&nbsp;problems"<br /></span><span style="color: #0000BB">?&gt;</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">&lt;?php<br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$xml&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</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">/*&nbsp;For&nbsp;each&nbsp;&lt;movie&gt;&nbsp;node,&nbsp;we&nbsp;echo&nbsp;a&nbsp;separate&nbsp;&lt;plot&gt;.&nbsp;*/<br /></span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$movie</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$movie</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">plot</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&lt;br&nbsp;/&gt;'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</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">&lt;?php<br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$xml&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</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">/*&nbsp;Access&nbsp;the&nbsp;&lt;rating&gt;&nbsp;nodes&nbsp;of&nbsp;the&nbsp;first&nbsp;movie.<br />&nbsp;*&nbsp;Output&nbsp;the&nbsp;rating&nbsp;scale,&nbsp;too.&nbsp;*/<br /></span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">rating&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$rating</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;switch((string)&nbsp;</span><span style="color: #0000BB">$rating</span><span style="color: #007700">[</span><span style="color: #DD0000">'type'</span><span style="color: #007700">])&nbsp;{&nbsp;</span><span style="color: #FF8000">//&nbsp;Get&nbsp;attributes&nbsp;as&nbsp;element&nbsp;indices<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">case&nbsp;</span><span style="color: #DD0000">'thumbs'</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$rating</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&nbsp;thumbs&nbsp;up'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #DD0000">'stars'</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$rating</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&nbsp;stars'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</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">&lt;?php&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$xml&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</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&nbsp;((string)&nbsp;</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">title&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #DD0000">'PHP:&nbsp;Behind&nbsp;the&nbsp;Parser'</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #DD0000">'My&nbsp;favorite&nbsp;movie.'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">((string)&nbsp;</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">title</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</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">&lt;?php<br />$el1&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</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&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</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&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">$el2</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;false&nbsp;since&nbsp;PHP&nbsp;5.2.0<br /></span><span style="color: #0000BB">?&gt;</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>&lt;character&gt;</i> elements:
    </p></div>
    <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$xml&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</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&nbsp;(</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">xpath</span><span style="color: #007700">(</span><span style="color: #DD0000">'//character'</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">$character</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$character</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'played&nbsp;by&nbsp;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$character</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">actor</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&lt;br&nbsp;/&gt;'</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents simpara"><p>
     &#039;<i>//</i>&#039; 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&#039;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">&lt;?php<br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$xml&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</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">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">characters</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">character</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Miss&nbsp;Coder'</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">asXML</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</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">&lt;?php<br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">'example.php'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$xml&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</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&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">characters</span><span style="color: #007700">-&gt;</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">-&gt;</span><span style="color: #0000BB">addChild</span><span style="color: #007700">(</span><span style="color: #DD0000">'name'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Mr.&nbsp;Parser'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$character</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addChild</span><span style="color: #007700">(</span><span style="color: #DD0000">'actor'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'John&nbsp;Doe'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$rating&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">movie</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">addChild</span><span style="color: #007700">(</span><span style="color: #DD0000">'rating'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'PG'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$rating</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addAttribute</span><span style="color: #007700">(</span><span style="color: #DD0000">'type'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mpaa'</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">$xml</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">asXML</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</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">&lt;?php<br />$dom&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">domDocument</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$dom</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">loadXML</span><span style="color: #007700">(</span><span style="color: #DD0000">'&lt;books&gt;&lt;book&gt;&lt;title&gt;blah&lt;/title&gt;&lt;/book&gt;&lt;/books&gt;'</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$dom</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Error&nbsp;while&nbsp;parsing&nbsp;the&nbsp;document'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />}<br /><br /></span><span style="color: #0000BB">$s&nbsp;</span><span style="color: #007700">=&nbsp;</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&nbsp;</span><span style="color: #0000BB">$s</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">book</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">title</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div><p>
  </p>
 </div><?php manual_footer(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites