0

我无法使用 PHP 从 XML 文件中删除记录。

我不断收到以下错误:

致命错误:无法在第 19 行的 /Applications/XAMPP/xamppfiles/htdocs/catalogue/deleteaction.php 中使用 DOMElement 类型的对象作为数组

这是代码:

XML

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="catalogue_overview.xsl"?>

<catalogue>
  <record>
    <catId>001</catId>
    <title>Fungus</title>
    <location>GP</location>
    <photographer>jrm</photographer>
    <equipment>Canon EOS 40D</equipment>
    <caption>Small fungus</caption>
    <notes>This is a very rare species of fungus</notes>
    <date>10/8/2012</date>
    <imageUrl>images/IMG_1684.jpg</imageUrl>
  </record>
</catalogue>

PHP cataloguedelete.php

!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Photo Catalogue - Delete Entry</title>

<link rel="stylesheet" type="text/css" href="css/style.css"/>

</head>

<body>


    <h1>Delete Entry From Catalogue</h1>

    <p>DDDDDDD</p>

<?
echo "<form action='deleteaction.php' method='post'>\n";
$doc = new DOMDocument();
$doc->load('catalogue.xml');
$catdelete = $doc->getElementsByTagName("record");
foreach($catdelete as $record) {

  $catIds = $record->getElementsByTagName( "catId" );
  $catId = $catIds->item(0)->nodeValue;

  $titles = $record->getElementsByTagName( "title" );
  $title = $titles->item(0)->nodeValue;

  $locations = $record->getElementsByTagName( "location" );
  $location = $locations->item(0)->nodeValue;

  $photographers = $record->getElementsByTagName( "photographer" );
  $photographer = $photographers->item(0)->nodeValue;

  $equip = $record->getElementsByTagName( "equipment" );
  $equipment = $equip->item(0)->nodeValue;

  $captions = $record->getElementsByTagName( "caption" );
  $caption = $captions->item(0)->nodeValue;

  $note = $record->getElementsByTagName( "notes" );
  $notes = $note->item(0)->nodeValue;

  $dates = $record->getElementsByTagName( "date" );
  $date = $dates->item(0)->nodeValue;

  echo "<input type='checkbox' name='EntriesToRemove[]' value='" . $catId . "'> $title, &quot;$location&quot;<br>\n";
}
echo "<br><input type='submit' value='Delete Entry' />\n</form>\n";
?>

</body>
</html>

删除动作.php

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>

<body>

<?
$entries_to_remove = $_POST['EntriesToRemove'];


$doc = new DOMDocument();
$doc->load("catalogue.xml");

$catalogue = $doc->getElementsByTagName("catalogue");
foreach($catalogue as $record) {
    if($record['catId'] == $_POST["EntriesToRemove"]) {
        // unset($record);
        $catalogue->removeChild($record);
    }
}
$doc->save("catalogue.xml");
?>

</body>
</html>

错误在线:

if($record['catId'] == $_POST["EntriesToRemove"]) {

我确定这很简单,我错过了

任何建议将不胜感激

非常感谢

4

1 回答 1

1

您已设置EntriesToRemove为 HTML 中的数组 - 因此您需要检查是否catId是该数组的元素而不是等于它(如您链接的答案中所建议的那样)。但是您还需要正确地从 DOMDocument 中获取 catId 值(它不是数组)——您实际上可以使用您在 HTML 中使用的代码来执行此操作,如下所示:

foreach($catalogue as $record) {
    // Get the CatIds from this record
    $catIds = $record->getElementsByTagName( "catId" );
    // get the first CatId (you might actually want to check this exists
    // first if you can't be sure the XML contains that key)
    $catId = $catIds->item(0)->nodeValue;
    // Look to see if the given catId is in the $_POST array
    if (in_array($catId, $_POST['EntriesToRemove'])) {
        // Found it!
    }
}

...应该做的伎俩。

编辑我们循环访问<record>条目的方式也存在问题。而且,由于执行 removeChild 时修改 DOMDocument 的方式,您无法在foreach循环中间删除(请参阅此处的注释:DOMNode::removeChild)。相反,您需要做的是存储要删除的节点的副本,然后在循环之外将其删除。

所以,我们最终得到的是:

$catalogue = $doc->getElementsByTagName("catalogue");
$records= $doc->getElementsByTagName('record');
$nodeToRemove= null;

foreach($records as $record) {
    // Get the CatIds from this record
    $catIds = $record->getElementsByTagName( "catId" );
    // get the first CatId (you might actually want to check this exists
    // first if you can't be sure the XML contains that key)
    $catId = $catIds->item(0)->nodeValue;
    // Look to see if the given catId is in the $_POST array
    if (in_array($catId, $_POST['EntriesToRemove'])) {
        // Found it! Store it for removal
        $nodeToRemove= $record;
    }
}

if ($nodeToRemove!=null) {
    $oldnode= $nodeToRemove->parentNode->removeChild($nodeToRemove);
}
$doc->save('catalogue.xml');
于 2013-10-23T07:21:09.220 回答