Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The </p> <pre><code>&lt;?php $xmlstr = '&lt;?xml version="1.0" standalone="yes"?&gt; &lt;table count="" time="0.010006904602051"&gt; &lt;item&gt; &lt;id&gt;607&lt;/id&gt; &lt;name&gt;MSPOT6071&lt;/name&gt; &lt;description&gt;Hip Hop / Raps&lt;/description&gt; &lt;type&gt;3&lt;/type&gt; &lt;radio_folder_id/&gt; &lt;albumart&gt; http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_175.jpg &lt;/albumart&gt; &lt;albumart_300&gt; http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_350.jpg &lt;/albumart_300&gt; &lt;albumart_500&gt; http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_500.jpg &lt;/albumart_500&gt; &lt;/item&gt; &lt;item&gt; &lt;id&gt;48542614&lt;/id&gt; &lt;name&gt;US Pop - TESTB&lt;/name&gt; &lt;description&gt;Blues&lt;/description&gt; &lt;type&gt;3&lt;/type&gt; &lt;radio_folder_id/&gt; &lt;/item&gt; &lt;/table&gt;'; $xml = new SimpleXMLElement($xmlstr); foreach($xml-&gt;item as $item) { echo $item-&gt;name."&lt;br&gt;"; } echo $xml-&gt;item[0]-&gt;name; echo '&lt;pre&gt;'; print_r($xml); echo '&lt;/pre&gt;'; ?&gt; </code></pre> <p>Assign your XML string to a variable $xmlstr and so you don't run into incomplete XML document errors make sure you include the following at the top of your XML document. </p> <pre><code>&lt;?xml version="1.0" standalone="yes"?&gt; </code></pre> <p>Then use the built-in SimpleXML class by passing your XML string $xmlstr to SimpleXML:</p> <pre><code>$xml = new SimpleXMLElement($xmlstr); </code></pre> <p>Now you can access your XML file as a PHP object using the attributes and methods of the SimpleXML class. In this example we loop over the 'item'(s) in your XML document and print out the 'name' elements:</p> <pre><code>foreach($xml-&gt;item as $item) { echo $item-&gt;name."&lt;br&gt;"; } </code></pre> <p>I have also include code to access the first item element:</p> <pre><code>echo $xml-&gt;item[0]-&gt;name; </code></pre> <p>As well as some debugging code view the XML document in a SimpleXML object:</p> <pre><code>echo '&lt;pre&gt;'; print_r($xml); echo '&lt;/pre&gt;'; </code></pre> <p>You access the key or in this case the object property by its name. So in your foreach loop you might do:</p> <pre><code>if($item-&gt;name) { echo $item-&gt;name; } </code></pre> <p>or</p> <pre><code>if($item-&gt;description) { echo $item-&gt;description; } </code></pre> <p>May the force be with you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload