我想根据用户输入删除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" >
我搜遍了所有,并没有找到基于uer条目删除数据的很多内容。有没有更好的方法来删除元素?我不明白我错过了什么。
答案 0 :(得分:0)
以下XPath查询与XML结构不对应:
/YealinkIPPhone/DirectoryEntry[a = '$aTag']
即,名为YealinkIPPhoneDirectory
的XML的根元素,而不是YealinkIPPhone
。 DirectoryEntry
元素的子项需要评估的值为Name
而不是a
:
foreach($xpath->query("/YealinkIPPhoneDirectory/DirectoryEntry[Name = '$aTag']") as $node)
{
$node->parentNode->removeChild($node);
}
<强> eval.in demo
强>