0
我有一些这样的XML:删除父节点从XML在PHP
<tree path="Masters">
<item id="Masters\2014" name="2014" isFolder="true" path="Masters\2014" >
<item id="Masters\2014\Brochures" name="Brochures" isFolder="true" path="Masters\2014\Brochures" >
<item id="Masters\2014\Brochures\PLEASE DO NOT COPY" name="PLEASE DO NOT COPY" isFolder="true" path="Masters\2014\Brochures\PLEASE DO NOT COPY" >
<item id="a4e6f520-9b26-42c0-af92-bbd17ab6e8b6" name="00001" isFolder="false" path="Masters\2014\Brochures\PLEASE DO NOT COPY\00001.xml" >
<fileInfo fileSize="141.23 Kb"></fileInfo>
</item>
<item id="6b8cbff5-cf03-4d2c-9931-bb58d7f3ff8a" name="00002" isFolder="false" path="Masters\2014\Brochures\PLEASE DO NOT COPY\00002.xml" >
<fileInfo fileSize="192.19 Kb"></fileInfo>
</item>
</item>
<item id="65773008-4e64-4316-92dd-6a535616ccf6" name="Sales Brochure A4" isFolder="false" path="Masters\2014\Brochures\Sales Brochure A4.xml" >
<fileInfo fileSize="34.38 Kb"></fileInfo>
</item>
</item>
</item>
</tree>
我需要删除所有节点(包括小孩),其中属性name
正则表达式匹配/^[0-9]{5,6}$/
(它是一个5或6数字长名称),也删除其父母。
除此之外,我还需要删除任何具有属性isFolder
设置为false
的元素。
我到目前为止的代码是:
<?php
$simple_xml = simplexml_load_string($xml);
//Foreach item tag
foreach($simple_xml->xpath('//item') as $item) {
//This correctly identifies the nodes
if(preg_match('/^[0-9]{5,6}$/', $item->attributes()->name)) {
//This doesn't work. I'm guessing chaining isn't possible?
$dom = dom_import_simplexml($item);
$dom->parentNode->parentNode->removeChild($dom);
} else {
//This correctly identifies the nodes
if($item->attributes()->isFolder == 'false') {
//This part works correctly and removes the nodes as required
$dom = dom_import_simplexml($item);
$dom->parentNode->removeChild($dom);
}
}
}
//At this point $simple_xml should contain the rebuilt xml tree in simplexml style
?>
由于可以从评论中可以看出,我有isFolder
部分工作,我需要,但我似乎不能当删除父节点项目节点的属性name
的值为5或6位数的长名称。
在此先感谢您的帮助。
完美!感谢您的及时解决方案。我知道这会很简单。 – PaulSkinner 2014-10-29 16:03:35