Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>While <a href="http://de.php.net/manual/en/book.simplexml.php" rel="nofollow noreferrer">SimpleXML</a> provides <a href="https://stackoverflow.com/a/16062633/367456">a way to remove</a> XML nodes, its modification capabilities are somewhat limited. One other solution is to resort to using the <a href="http://de.php.net/manual/en/book.dom.php" rel="nofollow noreferrer">DOM</a> extension. <a href="http://de.php.net/manual/en/function.dom-import-simplexml.php" rel="nofollow noreferrer">dom_import_simplexml()</a> will help you with converting your <code>SimpleXMLElement</code> into a <code>DOMElement</code>.</p> <p>Just some example code (tested with PHP 5.2.5):</p> <pre><code>$data='&lt;data&gt; &lt;seg id="A1"/&gt; &lt;seg id="A5"/&gt; &lt;seg id="A12"/&gt; &lt;seg id="A29"/&gt; &lt;seg id="A30"/&gt; &lt;/data&gt;'; $doc=new SimpleXMLElement($data); foreach($doc-&gt;seg as $seg) { if($seg['id'] == 'A12') { $dom=dom_import_simplexml($seg); $dom-&gt;parentNode-&gt;removeChild($dom); } } echo $doc-&gt;asXml(); </code></pre> <p>outputs</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;data&gt;&lt;seg id="A1"/&gt;&lt;seg id="A5"/&gt;&lt;seg id="A29"/&gt;&lt;seg id="A30"/&gt;&lt;/data&gt; </code></pre> <p>By the way: selecting specific nodes is much more simple when you use XPath (<a href="http://de.php.net/manual/en/function.simplexml-element-xpath.php" rel="nofollow noreferrer">SimpleXMLElement->xpath</a>): </p> <pre><code>$segs=$doc-&gt;xpath('//seq[@id="A12"]'); if (count($segs)&gt;=1) { $seg=$segs[0]; } // same deletion procedure as above </code></pre>
 

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