【发布时间】:2023-04-10 11:41:01
【问题描述】:
我想根据用户输入删除一个 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 条目删除数据的内容。有没有更好的方法来删除元素?我没有看到我错过了什么。
【问题讨论】:
-
是它的根元素,谢谢!您对命名元素的评估是正确的。不知道我的帖子是如何恢复的。我很欣赏第二双眼睛。标记为答案。
标签: php xml input element removechild