本文介绍了如何使用PHP XML Dom中的属性值删除特定节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是最佳短语:

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

除非我不使用simpleXML。

except I'm not using simpleXML.

我是PHP的新手,因此我可能不会做最好的方法。

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

我有一个使用$ dom- >为每个用户保存($ xml)。 (由于未公开的原因,不会将其全部放在一个xml中)

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

它给了我xml声明<?xml version =1.0?> ; (不知道如何使其他人,但这不是重点,希望)

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>

而且我想删除一个类别,该类别具有名称为id的特定属性,并在php中使用DOM类我运行一个使用删除按钮激活的功能。

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 );
    }

以上打印出一个[0] - > 0的数组时间,当我插槽返回外面if。
有更好的方法吗?我也尝试过使用getElementbyId,但我不知道如何工作。

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.

推荐答案

好的,让我们试试这个完整的例子:

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>';

这篇关于如何使用PHP XML Dom中的属性值删除特定节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 06:49