Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>PHP 5.1.2+ has an extension called <a href="http://php.net/manual/en/book.simplexml.php" rel="nofollow">SimpleXML</a> enabled by default. It's very useful for parsing well-formed XML like your example above.</p> <p>First, create a <a href="http://www.php.net/manual/en/class.simplexmlelement.php" rel="nofollow">SimpleXMLElement</a> instance, passing the XML to its constructor. SimpleXML will parse the XML for you. (This is where I feel the elegance of SimpleXML lies - SimpleXMLElement is the entire library's sole class.)</p> <pre><code>$xml = new SimpleXMLElement($yourXml); </code></pre> <p>Now, you can easily traverse the XML as if it were any PHP object. Attributes are accessible as array values. Since you're looking for tags with specific attribute values, we can write a simple loop to go through the XML:</p> <pre><code>&lt;?php $yourXml = &lt;&lt;&lt;END &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;test&gt; &lt;required&gt; &lt;item type="binary"&gt; &lt;name&gt;The name&lt;/name&gt; &lt;url visibility="restricted"&gt;c:/temp/test/widget.exe&lt;/url&gt; &lt;/item&gt; &lt;item type="document" subtype="mytype"&gt; &lt;name&gt;document name&lt;/name&gt; &lt;url visiblity="visible"&gt;c:/temp/test.pdf&lt;/url&gt; &lt;/item&gt; &lt;/required&gt; &lt;/test&gt; END; // Create the SimpleXMLElement $xml = new SimpleXMLElement($yourXml); // Store an array of results, matching names to URLs. $results = array(); // Loop through all of the tests foreach ($xml-&gt;required[0]-&gt;item as $item) { if ( ! isset($item['subtype']) || $item['subtype'] != 'mytype') { // Skip this one. continue; } // Cast, because all of the stuff in the SimpleXMLElement is a SimpleXMLElement. $results[(string)$item-&gt;name] = (string)$item-&gt;url; } print_r($results); </code></pre> <p>Tested to be correct in <a href="http://codepad.org/DBJY2wer" rel="nofollow">codepad</a>.</p> <p>Hope this helps!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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