Removed all junk nodes in xml using php?
<p>Removed ALL junk nodes in xml using php</p>

<p>This is the sample input for the example:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;root&gt;
    &lt;!--comment1--&gt;
    &lt;elem&gt;a&lt;/elem&gt;
    &lt;junk&gt;b&lt;/junk&gt;
    &lt;elem&gt;
        &lt;!--comment2--&gt;
        &lt;junk&gt;c&lt;junk&gt;d&lt;/junk&gt;&lt;/junk&gt;
    &lt;/elem&gt;
    &lt;!--comment3--&gt;
    &lt;junk&gt;e&lt;/junk&gt;
&lt;/root&gt;
</code></pre>

<p>This is the resulting XML:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;root&gt;

    &lt;elem&gt;a&lt;/elem&gt;

    &lt;elem&gt;


    &lt;/elem&gt;


&lt;/root&gt;
</code></pre>

<p>I look documents and applied like this:</p>

<pre><code>$doc = new DOMDocument();
$doc-&gt;loadXML($xml);
$xpath = new DOMXPath($doc);

foreach ($xpath-&gt;query('/root/') as $elem) {
    $elem-&gt;parentNode-&gt;removeChild($elem);
}
</code></pre>

<p>for my case will remove all node "junk" and they will everywhere in xml document.</p>
