dpjs2005 2009-01-19 10:21
浏览 61
已采纳

如何在PHP XML Dom中使用其属性值删除特定节点?

My question is best phrase as: Remove a child with a specific attribute, in SimpleXML for PHP

except I'm not using simpleXML.

I'm new to XML for PHP so I may not be doing the best way

I have a xml created using the $dom->save($xml) for each individual user. (not placing all in one xml due to undisclosed reasons)

It gives me that xml declaration <?xml version="1.0"?> (no idea how to make it to others, but that's not the point, hopefully)

<?xml version="1.0"?>
<details>
 <person>name</person>
 <data1>some data</data1>
 <data2>some data</data2>
 <data3>some data</data3>
 <category id="0">
  <categoryName>Cat 1</categoryName>
  <categorydata1>some data</categorydata1>
 </category>
 <category id="1">
  <categoryName>Cat 2</categoryName>
  <categorydata1>some data</categorydata1>
  <categorydata2>some data</categorydata2>
  <categorydata3>some data</categorydata3>
  <categorydata4>some data</categorydata4>
 </category>
</details>

And I want to remove a category that has a specific attribute named id with the DOM class in php when i run a function activated from using a remove button.

the following is the debug of the function im trying to get to work. Can i know what I'm doing wrong?

function CatRemove($myXML){
        $xmlDoc = new DOMDocument();
        $xmlDoc->load( $myXML );
        $categoryArray = array();

        $main = $xmlDoc->getElementsByTagName( "details" )->item(0);

        $mainElement = $xmlDoc->getElementsByTagName( "details" );
        foreach($mainElement as $details){
            $currentCategory = $details->getElementsByTagName( "category" );
            foreach($currentCategory as $category){
                $categoryID = $category->getAttribute('id');
                array_push($categoryArray, $categoryID);
                if($categoryID == $_POST['categorytoremoveValue']) {
                    return $categoryArray;
                }
            }
        }


        $xmlDoc->save( $myXML );
    }

Well the above prints me an array of [0]->0 all the time when i slot the return outside the if. is there a better way? I've tried using getElementbyId as well but I've no idea how to work that.

I would prefer not to use an attribute though if that would make things easier.

  • 写回答

4条回答 默认 最新

  • dongqie2355 2009-01-19 16:10
    关注

    Ok, let’s try this complete example of use:

    function CatRemove($myXML, $id) {
        $xmlDoc = new DOMDocument();
        $xmlDoc->load($myXML);
        $xpath = new DOMXpath($xmlDoc);
        $nodeList = $xpath->query('//category[@id="'.(int)$id.'"]');
        if ($nodeList->length) {
            $node = $nodeList->item(0);
            $node->parentNode->removeChild($node);
        }
        $xmlDoc->save($myXML);
    }
    
    // test data
    $xml = <<<XML
    <?xml version="1.0"?>
    <details>
     <person>name</person>
     <data1>some data</data1>
     <data2>some data</data2>
     <data3>some data</data3>
     <category id="0">
      <categoryName>Cat 1</categoryName>
      <categorydata1>some data</categorydata1>
     </category>
     <category id="1">
      <categoryName>Cat 2</categoryName>
      <categorydata1>some data</categorydata1>
      <categorydata2>some data</categorydata2>
      <categorydata3>some data</categorydata3>
      <categorydata4>some data</categorydata4>
     </category>
    </details>
    XML;
    // write test data into file
    file_put_contents('untitled.xml', $xml);
    // remove category node with the id=1
    CatRemove('untitled.xml', 1);
    // dump file content
    echo '<pre>', htmlspecialchars(file_get_contents('untitled.xml')), '</pre>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b