我有一个问题,林根据一个孩子值试图删除元素(孩子的): 这里是XML示例:
<?xml version="1.0" encoding="UTF-8"?>
<Businesses>
<Business NAME="busin1">
<CHILD ID="30"><title>child Title</title>
<description>Child Description</description>
<urlToSite>http://www.MySite.co.il</urlToSite>
<endtime>20120720103000</endtime>
<starttime>20120710191500</starttime>
</CHILD>
<CHILD>...</CHILD>
<CHILD>...</CHILD>
</Business>
</Businesses>
现在我需要删除所有特定的“儿童“元(已包含儿童),其‘结束时间’值是旧的,然后现在(或简称‘结束时间’等于特定值)
‘结束时间’与格式的日期:YYYYMMDDHHMMSS
这里我的第一次尝试(没有成功):
$doc = new DOMDocument;
$doc->load('MyXML.xml'); //The XML Above
$thedocument = $doc->documentElement;
//this gives you a list of the childs
$list = $thedocument->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->childNodes->item('endtime')->nodeValue; //not Sure about this!!
if ($attrValue > $time) {
$nodeToRemove = $domElement;
}
}
//Now remove it.
if ($nodeToRemove != null)
$thedocument->removeChild($nodeToRemove);
echo $doc->saveXML();
非常感谢!
嗨!我试过你的代码,它仍然无法正常工作:(我无法理解为什么...我在你的代码中发现的唯一问题是> {$ maxTime}应该是<{$ maxTime}它找到正确的CHILD ID(我添加了:“echo $ businessNode-> getAttribute('ID');”我可以看到正确的CHILD ID,但它不会将它从节点中删除,我添加了“echo $ doc-> saveXml() ;“最后以及...任何建议??谢谢! – EranLevi 2012-07-28 14:23:19