1

我的问题是最好的短语: Remove a child with a specific attribute, in SimpleXML for PHP

除了我没有使用 simpleXML。

我是 PHP 的 XML 新手,所以我可能不是最好的方法

我为每个单独的用户使用 $dom->save($xml) 创建了一个 xml。(由于未公开的原因,未将所有内容放在一个 xml 中)

它给了我那个 xml 声明<?xml version="1.0"?>(不知道如何将它传递给其他人,但这不是重点,希望如此)

<?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 类的类别。

以下是我试图开始工作的函数的调试。我能知道我做错了什么吗?

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

好吧,当我将返回插入 if 之外时,上面一直打印出一个 [0]->0 的数组。有没有更好的办法?我也尝试过使用 getElementbyId ,但我不知道如何工作。

我宁愿不使用属性,但如果这会使事情变得更容易。

4

4 回答 4

4

好的,让我们试试这个完整的使用示例:

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>';
于 2009-01-19T16:10:51.937 回答
1

所以你想删除category一个特定的节点id

$node = $xmlDoc->getElementById("12345");
if ($node) {
    $node->parentNode->removeChild($node);
}

您还可以使用 XPath 来获取节点,例如:

$xpath = new DOMXpath($xmlDoc);
$nodeList = $xpath->query('//category[@id="12345"]');
if ($nodeList->length) {
    $node = $nodeList->item(0);
    $node->parentNode->removeChild($node);
}

我没有测试过它,但它应该可以工作。

于 2009-01-19T10:41:22.710 回答
0

你可以试试这个修改后的版本:

function CatRemove($myXML, $id){
    $doc = new DOMDocument();
    $doc->loadXML($myXML);
    $xpath = new DOMXpath($doc);
    $nodeList = $xpath->query("//category[@id='$id']");
    foreach ($nodeList as $element) {
        $element->parentNode->removeChild($element);
    }

    echo htmlentities($doc->saveXML());
}

它对我有用。只需根据您的需要进行调整。它不打算按原样使用,而只是概念证明。您还必须从字符串中删除 xml 声明。

于 2009-01-19T15:07:30.253 回答
0

修改上述功能以从邮件列表中删除电子邮件

function CatRemove($myXML, $id) {
    $xmlDoc = new DOMDocument();
    $xmlDoc->load($myXML);
    $xpath = new DOMXpath($xmlDoc);
    $nodeList = $xpath->query('//subscriber[@email="'.$id.'"]');
    if ($nodeList->length) {
        $node = $nodeList->item(0);
        $node->parentNode->removeChild($node);
    }
    $xmlDoc->save($myXML);
}
$xml = 'list.xml';
$to = $_POST['email'];//user already submitted they email using a form 
CatRemove($xml,$to);
于 2010-05-12T12:03:29.597 回答