Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP XML: removing Element and all children according to node value?
    primarykey
    data
    text
    <p><strong>Hello!</strong></p> <p>i have a problem, Im trying to remove Element (With childs) according to one Child value: Here is the XML Sample:</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;Businesses&gt; &lt;Business NAME="busin1"&gt; &lt;CHILD ID="30"&gt;&lt;title&gt;child Title&lt;/title&gt; &lt;description&gt;Child Description&lt;/description&gt; &lt;urlToSite&gt;http://www.MySite.co.il&lt;/urlToSite&gt; &lt;endtime&gt;20120720103000&lt;/endtime&gt; &lt;starttime&gt;20120710191500&lt;/starttime&gt; &lt;/CHILD&gt; &lt;CHILD&gt;...&lt;/CHILD&gt; &lt;CHILD&gt;...&lt;/CHILD&gt; &lt;/Business&gt; &lt;/Businesses&gt; </code></pre> <p>Now i need to remove All of the specific "CHILD" element (Incl. children) that its "endtime" value is older then now (Or simply "endtime" is equal to specific value)</p> <p>"endtime" is a date with the format: yyyymmddHHMMSS</p> <p>Here is my first try (without success) :</p> <pre><code> $doc = new DOMDocument; $doc-&gt;load('MyXML.xml'); //The XML Above $thedocument = $doc-&gt;documentElement; //this gives you a list of the childs $list = $thedocument-&gt;getElementsByTagName('CHILD'); //figure out which ones you want -- assign it to a variable (ie: $nodeToRemove ) $nodeToRemove = null; $time=date("YmdHis",time ()); foreach ($list as $domElement){ $attrValue = $domElement-&gt;childNodes-&gt;item('endtime')-&gt;nodeValue; //not Sure about this!! if ($attrValue &gt; $time) { $nodeToRemove = $domElement; } } //Now remove it. if ($nodeToRemove != null) $thedocument-&gt;removeChild($nodeToRemove); echo $doc-&gt;saveXML(); </code></pre> <p><strong>Thank you Very Much!</strong></p>
    singulars
    1. This table or related slice is empty.
    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. 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