2016-08-31 6 views
1

ユーザー入力に基づいてXMLエントリを削除します。ユーザー入力に基づいて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> 

私は下の2番目のフォームを配置し、私は結果が追加されないか、削除行くの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のエントリに基づいて、データの除去に多くを見つけるdidntの。要素を削除するより良い方法はありますか?私は何が欠けているのか分からない。

+0

ルート要素は、それはあなたに感謝でした!あなたは、指定された要素の評価で正しいです。それが私のポストでどのように元に戻ったかわからない。私は目の2番目のセットに感謝します。答えとしてマークされています。 –

答えて

0

次のXPathクエリは、XML構造に対応していません。

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

つまり、XMLのルート要素がYealinkIPPhoneYealinkIPPhoneDirectoryをしませ命名します。そして、評価する必要が値DirectoryEntry要素の子がNameないaです:

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

eval.in demo

関連する問題