如何在 PHP 中删除 XML 的节点 [重复]
Posted
技术标签:
【中文标题】如何在 PHP 中删除 XML 的节点 [重复]【英文标题】:How delete nodes of XML in PHP [duplicate] 【发布时间】:2013-10-21 08:18:22 【问题描述】:我需要删除字符串中 XML 的前 N 个节点。
$xml = new SimpleXMLElement($xmlString, LIBXML_NOCDATA);
$node = $nodes->xpath("/data/event");
$total_nodes = count($node);
$delete_n_rows = $total_nodes-$N;
for($i=0; $i<$delete_n_rows; $i++)
if ( ! empty($node))
unset($node[i]);
但该代码不适用于字符串内的这个 xml。
<data profile='color'><scale mode='month' today='February 2010'><x>
<column><![CDATA[Monday]]></column>
<column><![CDATA[Tuesday]]></column>
<column><![CDATA[Wednesday]]></column>
<column><![CDATA[Thursday]]></column>
<column><![CDATA[Friday]]></column>
<column><![CDATA[Saturday]]></column>
<column><![CDATA[Sunday]]></column></x>
<row height='224'><![CDATA[01|02|03|04|05|06|07]]></row>
<row height='224'><![CDATA[08|09|10|11|12|13|14]]></row>
<row height='224'><![CDATA[15|16|17|18|19|20|21]]></row>
<row height='310'><![CDATA[22|23|24|25|26|27|28]]></row></scale>
<event week='4' day='3' type='event_clear' x='300' y='672' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 06:00 Wicked]]></body></event>
<event week='4' day='3' type='event_clear' x='300' y='696' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 07:00 Ben Hur Live]]></body></event>
<event week='4' day='3' type='event_clear' x='300' y='720' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 07:00 Giselle]]></body></event>
<event week='4' day='3' type='event_clear' x='300' y='744' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 08:00 Billy Connolly]]></body></event>
<event week='4' day='3' type='event_clear' x='300' y='768' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 09:00 Giselle]]></body></event>
<event week='4' day='3' type='event_clear' x='300' y='792' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 10:00 Legally Blonde The Musical1]]></body></event>
<event week='4' day='3' type='event_clear' x='300' y='816' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 12:00 Giselle]]></body></event>
<event week='4' day='3' type='event_clear' x='300' y='840' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 12:00 Legally Blonde The Musical2]]></body></event>
<event week='4' day='3' type='event_clear' x='300' y='864' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 13:00 Legally Blonde The Musical3]]></body></event>
<event week='4' day='3' type='event_clear' x='300' y='888' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 14:00 Wicked]]></body></event>
<event week='4' day='3' type='event_clear' x='300' y='912' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 16:00 Billy Connolly]]></body></event>
<event week='4' day='3' type='event_clear' x='300' y='936' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 22:00 Jersey Boys]]></body></event></data>
如您所见,我正在尝试使用 SimpleXMLElement,但我也会对任何其他解决方案感到满意。
【问题讨论】:
不确定该链接是否提供了我的答案,但我终于明白了。请注意,在我的问题中,事件没有 ID 字段,所以有点棘手。 是的,id
没有你需要的答案,但我看到你确实明白了:)
【参考方案1】:
好吧,当我没有 ID 并尝试删除前 N 个节点时,这就是最终对我有用的 sn-p。
//Where this->xml is my x
$node = $this->xml->xpath("/data/event");
$total_nodes = count($node)- $n_events;
for($i=0; $i<$total_nodes; $i++)
$dom=dom_import_simplexml($this->xml->event[0]);
$dom->parentNode->removeChild($dom);
//If you want back to String
//$this->xml->asXML(); //At this point I want a SimpleXML with a content that i wish,
【讨论】:
以上是关于如何在 PHP 中删除 XML 的节点 [重复]的主要内容,如果未能解决你的问题,请参考以下文章