最近,我一直在使用xml编码,并且出现了问题。
考虑到这样的xml:
<Colonel id="1">
<Lieutenant_Colonel id="2">
<Secretary id="6"/>
</Lieutenant_Colonel>
<Lieutenant_Colonel id="3">
<Secretary id="7"/>
</Lieutenant_Colonel>
<Secretary id="5"/>
</Colonel>现在上校(id=1)和秘书(id=5)一起走了
我们想要的xml就像
<!-- <Colonel id="1"> -->
<Lieutenant_Colonel id="2">
<Secretary id="6"/>
</Lieutenant_Colonel>
<Lieutenant_Colonel id="3">
<Secretary id="7"/>
</Lieutenant_Colonel>
<!--
<Secretary id="5"/>
</Colonel> -->或
<Lieutenant_Colonel id="2">
<Secretary id="6"/>
</Lieutenant_Colonel>
<Lieutenant_Colonel id="3">
<Secretary id="7"/>
</Lieutenant_Colonel>怎么做这件事?
请帮帮我
发布于 2010-11-20 06:17:49
这是一个相当长的他们,所以我将不得不参考其他问题。首先,考虑这一职能:
function RemoveColonel($id, $secretaryId)
{
$colXpath = '//colonel[@id="' . $id . '"]';
$secXpath = $colXpath . '/Secretary[@id="' . $secretaryId . '"]';
RemoveNode($secXpath);
$colChildren = $rootXml->xpath($colXpath)->children();
$children = array();
foreach ($colChildren as $child ){
$children[] = $child;
}
RemoveNode($colXpath);
return $children;
}考虑到上校的身份和上校的秘书身份,它删除了秘书,然后就把上校的孩子们救出,然后送回孩子们。之后,如果您愿意,您可以再次在根节点中插入元素。如果在删除上校时无法获得秘书Id,则可以通过移除id规范,绕过xpath将秘书作为上校的子级移除。
RemoveNode代码有点长,所以我必须向您介绍如何在this中使用SimpleXML删除节点。
我希望我能帮上忙!
https://stackoverflow.com/questions/4231067
复制相似问题