<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Basic SimpleXML usage</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="simplexml.examples.html">Examples</a></div> <div class="next" style="text-align: right; float: right;"><a href="simplexml.examples-errors.html">Dealing with XML errors</a></div> <div class="up"><a href="simplexml.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="simplexml.examples-basic" class="section"> <h2 class="title">Basic SimpleXML 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 <span class="function"><a href="function.simplexml-load-file.html" class="function">simplexml_load_file()</a></span>. </p> <p class="para"> <div class="example" id="example-5484"> <p><strong>Example #1 Include file example.php with XML string</strong></p> <div class="example-contents"> <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 class="para"> The simplicity of SimpleXML appears most clearly when one extracts a string or number from a basic XML document. <div class="example" id="example-5485"> <p><strong>Example #2 Getting <em><plot></em></strong></p> <div class="example-contents"> <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">$movies </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">$movies</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">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> 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. </pre></div> </div> </div> </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. <div class="example" id="example-5486"> <p><strong>Example #3 Getting <em><line></em></strong></p> <div class="example-contents"> <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">$movies </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">$movies</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">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> PHP solves all my web problems </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5487"> <p><strong>Example #4 Accessing non-unique elements in SimpleXML</strong></p> <div class="example-contents"><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"> <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">$movies </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 <character> node, we echo a separate <name>. */<br /></span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$movies</span><span style="color: #007700">-></span><span style="color: #0000BB">movie</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">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: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr </pre></div> </div> </div> </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Properties (<em>$movies->movie</em> in previous example) are not arrays. They are <a href="class.iterator.html" class="link">iterable</a> and <a href="class.arrayaccess.html" class="link">accessible</a> objects. </p> </p></blockquote> <p class="para"> <div class="example" id="example-5488"> <p><strong>Example #5 Using attributes</strong></p> <div class="example-contents"><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 <span class="type"><a href="language.types.array.html" class="type array">array</a></span>. </p></div> <div class="example-contents"> <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">$movies </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">$movies</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 class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> 7 thumbs up5 stars </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5489"> <p><strong>Example #6 Comparing Elements and Attributes with Text</strong></p> <div class="example-contents"><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 <em>(string)</em>. Otherwise, PHP treats the element as an object. </p></div> <div class="example-contents"> <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">$movies </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">$movies</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 />echo </span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">((string) </span><span style="color: #0000BB">$movies</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 class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> My favorite movie.PHP: Behind the Parser </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5490"> <p><strong>Example #7 Comparing Two Elements</strong></p> <div class="example-contents"><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"> <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">$movies1 </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">$movies2 </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">$movies1 </span><span style="color: #007700">== </span><span style="color: #0000BB">$movies2</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 class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> bool(false) </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5491"> <p><strong>Example #8 Using XPath</strong></p> <div class="example-contents"><p> SimpleXML includes built-in <acronym title="XML Path Language">XPath</acronym> support. To find all <em><character></em> elements: </p></div> <div class="example-contents"> <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">$movies </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">$movies</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: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p> '<em>//</em>' serves as a wildcard. To specify absolute paths, omit one of the slashes. </p></div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5492"> <p><strong>Example #9 Setting values</strong></p> <div class="example-contents"><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"> <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">$movies </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">$movies</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">$movies</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"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> <?xml version="1.0" standalone="yes"?> <movies> <movie> <title>PHP: Behind the Parser</title> <characters> <character> <name>Miss Coder</name> <actor>Onlivia Actora</actor> </character> <character> <name>Mr. Coder</name> <actor>El Act&#xD3;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> <great-lines> <line>PHP solves all my web problems</line> </great-lines> <rating type="thumbs">7</rating> <rating type="stars">5</rating> </movie> </movies> </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5493"> <p><strong>Example #10 Adding elements and attributes</strong></p> <div class="example-contents"><p> Since PHP 5.1.3, SimpleXML has had the ability to easily add children and attributes. </p></div> <div class="example-contents"> <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">$movies </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">$movies</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">$movies</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">$movies</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"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> <?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&#xD3;r</actor> </character> <character><name>Mr. Parser</name><actor>John Doe</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> <great-lines> <line>PHP solves all my web problems</line> </great-lines> <rating type="thumbs">7</rating> <rating type="stars">5</rating> <rating type="mpaa">PG</rating></movie> </movies> </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-5494"> <p><strong>Example #11 DOM Interoperability</strong></p> <div class="example-contents"><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"> <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">$books </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">$books</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 class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> blah </pre></div> </div> </div> </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="simplexml.examples.html">Examples</a></div> <div class="next" style="text-align: right; float: right;"><a href="simplexml.examples-errors.html">Dealing with XML errors</a></div> <div class="up"><a href="simplexml.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>