Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>With the <a href="http://de.php.net/manual/en/book.dom.php" rel="nofollow noreferrer">DOM extension</a> it should be something like this:</p> <pre><code>$dom = new DOMDocument; $dom-&gt;validateOnParse = TRUE; $dom-&gt;loadXML($xmlString); // or use -&gt;load('file.xml') $node = $dom-&gt;getElementById('foo'); echo $node-&gt;nodeValue; </code></pre> <p>See the manual on</p> <ul> <li><a href="http://de.php.net/manual/en/domdocument.getelementbyid.php" rel="nofollow noreferrer"><code>DOMDocument::getElementById</code></a> — Searches for an element with a certain id</li> </ul> <hr> <p>If it doesn't work with <code>getElementById</code> (which usually only happens if the DTD doesn't know the id attribute), you can still use <a href="http://de.php.net/manual/en/class.domxpath.php" rel="nofollow noreferrer">XPath</a> to do the query:</p> <pre><code>$xpath = new DOMXPath($dom); $nodes = $xpath-&gt;query('//Message[@id = "foo"]'); foreach($nodes as $node) { echo $node-&gt;nodeValue; } </code></pre> <p>Unlike <code>getElementById</code>, an <a href="http://de.php.net/manual/en/domxpath.query.php" rel="nofollow noreferrer">XPath query</a> always returns a <a href="http://de.php.net/manual/en/class.domnodelist.php" rel="nofollow noreferrer"><code>DOMNodeList</code></a>. It will be empty if the query didn't find any nodes.</p> <hr> <p>If the ID is a real XML ID, you can also use the id() function in XPath</p> <pre><code>$xpath = new DOMXPath($dom); $nodes = $xpath-&gt;query('id("foo")'); foreach($nodes as $node) { echo $node-&gt;nodeValue; } </code></pre> <p>See <a href="https://stackoverflow.com/questions/3405117/simplify-php-dom-xml-parsing-how/3405651#3405651">Simplify PHP DOM XML parsing - how?</a> for more details on XML IDs.</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