根据用户输入删除XML元素


Remove XML elements based on user input

我想根据用户输入删除一个XML条目。

XML的例子:

<YealinkIPPhoneDirectory>
<DirectoryEntry>
<Name>Test321</Name>
<Telephone>101</Telephone>
<Telephone>102</Telephone>
<Telephone>103</Telephone>
</DirectoryEntry>
<DirectoryEntry>
<Name>Test456</Name>
<Telephone>107</Telephone>
<Telephone>108</Telephone>
<Telephone>109</Telephone>
</DirectoryEntry>
<DirectoryEntry>
<Name>Test789</Name>
<Telephone>000</Telephone>
<Telephone>111</Telephone>
<Telephone>222</Telephone>
</DirectoryEntry>
</YealinkIPPhoneDirectory>

因此将条目DirectoryEntry删除到DirectoryEntry端。条目工作正常,并提交了格式正确的数据:

<?php 
if (isset($_REQUEST['ok'])) { 
$xml = new DOMDocument("1.0","UTF-8"); 
$xml->load("emergency.xml"); 
$rootTag = $xml->getElementsByTagName("YealinkIPPhoneDirectory")->item(0); 
$dataTag = $xml->createElement("DirectoryEntry"); 
$aTag = $xml->createElement("Name",$_REQUEST['a']); 
$bTag = $xml->createElement("Telephone",$_REQUEST['b']); 
$cTag = $xml->createElement("Telephone",$_REQUEST['c']);
$dTag = $xml->createElement("Telephone",$_REQUEST['d']);
$dataTag->appendChild($aTag); 
$dataTag->appendChild($bTag); 
$dataTag->appendChild($cTag);
$dataTag->appendChild($dTag);
$rootTag->appendChild($dataTag); 
$xml->save("emergency.xml");
 } 
 ?> 
 <form class="form-horizontal" action="emergencycontacts.php" method="post">
  <form class="form-horizontal" action="emergencycontacts.php" method="post">
<fieldset>
<!-- Form Name -->
<legend> </legend>
<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label">Full Name</label>  
  <div class="col-md-5">
 <input class="form-control input-md" required="" type="text" name="a" /> 
 <span class="help-block">Required</span>  
  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label">Extension</label>  
  <div class="col-md-2">
 <input class="form-control input-md" value=" " type="text" name="b" /> 
 <span class="help-block">Internal</span>  
  </div>
</div>
 <!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label">Direct Phone Line</label>  
  <div class="col-md-4">
 <input class="form-control input-md" value=" " type="text" name="c" /> 
 <span class="help-block">External</span>  
  </div>
</div>
 <!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label">Cellular Phone</label>  
  <div class="col-md-4">
 <input class="form-control input-md" value=" " type="text" name="d" />
 <span class="help-block">External</span>  
  </div>
</div>
 <!-- Button (Double) -->
<div class="form-group">
  <label class="col-md-4 control-label" for="button1id"></label>
  <div class="col-md-8">
    <input class="btn btn-primary" input type="submit" name="ok" value="Submit Entry" >

  </div>
</div>
</fieldset>
 </form>

当我在下面放置第二个表单并试图用foreach删除节点时,我没有添加或删除结果。表单保持原样:

                <p>Please fill in the below form to REMOVE a contact to the phone system.</p>
<?php 
if (isset($_REQUEST['ok2'])) { 
$xml = new DOMDocument("1.0","UTF-8"); 
$xml->load("emergency.xml"); 
$aTag = $_POST['a'];
$xpath = new DOMXPATH($xml);
foreach($xpath->query("/YealinkIPPhone/DirectoryEntry[a = '$aTag']") as $node)
{
    $node->parentNode->removeChild($node);
}
$xml->formatoutput = true;
$xml->save("emergency.xml");
 } 
 ?> 
 <form class="form-horizontal" action="emergencycontacts.php" method="post">
  <form class="form-horizontal" action="emergencycontacts.php" method="post">
<fieldset>
<!-- Form Name -->
<legend> </legend>
<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label">Full Name</label>  
  <div class="col-md-5">
 <input class="form-control input-md" required="" type="text" name="a" /> 
 <span class="help-block">Required</span>  
  </div>
</div>
 <!-- Button (Double) -->
<div class="form-group">
  <label class="col-md-4 control-label" for="button1id"></label>
  <div class="col-md-8">
    <input class="btn btn-primary" input type="submit" name="ok2" value="Delete Entry" >

我搜索了所有,并没有发现很多基于用户输入的数据删除。是否有更好的方法来删除元素?我不知道我错过了什么

下面的XPath查询不对应于XML结构:

/YealinkIPPhone/DirectoryEntry[a = '$aTag']

即XML的根元素命名为YealinkIPPhoneDirectory,而不是YealinkIPPhone。需要求值的DirectoryEntry元素的子元素是Name而不是a:

foreach($xpath->query("/YealinkIPPhoneDirectory/DirectoryEntry[Name = '$aTag']") as $node)
{
    $node->parentNode->removeChild($node);
}

eval.in demo