du90093662774150 2015-11-23 11:46
浏览 59
已采纳

使用PHP删除父DOM元素

I want to remove elements (testcases) from an .xml file when the externalid (child element of testcase) is < 1000.

Testcases are always in testsuites. There can be more than one level of testsuites. So this is my .xml:

<?xml version="1.0" encoding="UTF-8"?>
<testsuite id="" name="" >
<node_order><![CDATA[]]></node_order>
<details><![CDATA[]]></details> 
<testsuite id="14162" name="Suite1" >
<node_order><![CDATA[0]]></node_order>
<details><![CDATA[]]></details> 
<testsuite id="14037" name="Child Suite 1" >
<node_order><![CDATA[1]]></node_order>
<details><![CDATA[]]></details> 

<testcase internalid="14038" name="Testcase 1">
    <node_order><![CDATA[0]]></node_order>
    <externalid><![CDATA[790]]></externalid>      <---EXTERNAL ID
    <version><![CDATA[1]]></version>

    <steps>
     <step>
      <step_number><![CDATA[1]]></step_number>
      <actions><![CDATA[<p>Text</p>]]></actions>
      <expectedresults><![CDATA[<p>Text</p>
      <p>Text</p>]]></expectedresults>
      <execution_type><![CDATA[1]]></execution_type>
     </step>
    </steps>
</testcase>

<testcase internalid="14040" name="Testcase 2">
    <node_order><![CDATA[0]]></node_order>
    <externalid><![CDATA[791]]></externalid>      <---EXTERNAL ID
    <version><![CDATA[1]]></version>
    <steps>
     <step>
       <step_number><![CDATA[1]]></step_number>
       <actions><![CDATA[<p>Text</p>]]></actions>
       <expectedresults><![CDATA[<p>Text</p>
       <p>Text</p>]]></expectedresults>
       <execution_type><![CDATA[1]]></execution_type>
     </step>
    </steps>
</testcase>
</testsuite>
<testcase internalid="14042" name="Testcase 3">
    <node_order><![CDATA[0]]></node_order>
    <externalid><![CDATA[792]]></externalid>      <---EXTERNAL ID
    <version><![CDATA[1]]></version>
    <steps>
     <step>
        <step_number><![CDATA[1]]></step_number>
         <actions><![CDATA[<p>Text</p>]]></actions>
         <expectedresults><![CDATA[<p>Text</p>
         <p>Text</p>]]></expectedresults>
         <execution_type><![CDATA[1]]></execution_type>
     </step>
    </steps>
</testcase>
</testsuite>
</testsuite>

This is my php code, which won't remove the testcase.

<?php
$number = 1000;
$dom = new DOMDocument('1.0');
$dom->load('tc.xml');
$testcases = $dom->getElementsByTagName('testcase');

foreach($testcases as $tckey=>$tc)
{
    $externalID = $tc->childNodes->item(3)->textContent;
    if($externalID > 0 && $externalID <= $number)
    {
        unset($tc);

        //$tc->parentNode->removeChild($tc);
        //$tc->childNodes->item(3)->parentNode->removeChild($tc);
    }
}
echo $dom->saveXML();
?>

What is the problem here? Why does my code not remove the testcases which have the external id <1000? Even the method with selecting the parent element won't work for me (parentNode).

Any help is welcome.

  • 写回答

1条回答 默认 最新

  • dongxibeng5324 2015-11-23 13:54
    关注

    DOMNode::getElementsByTagName() returns a live result. The list actually changes if you remove nodes. You might want to check out DOMXpath::evaluate(). The result is not live and you can use conditions in the Xpath expressions.

    $xpath = new DOMXpath($dom);
    
    foreach ($xpath->evaluate('//testcase[externalid < 1000]') as $testCase) {
      $testCase->parentNode->removeChild($testCase);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 请求分析基于spring boot+vue的前后端分离的项目
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?