我想删除:
<newWord>
<Heb>צהוב</Heb>
<Eng>yellow</Eng>
</newWord>
来自:
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<newWord>
<Heb>מילה ראשונה</Heb>
<Eng>first word</Eng>
</newWord>
<newWord>
<Heb>צהוב</Heb>
<Eng>yellow</Eng>
</newWord>
</xml>
因此输出将是:
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<newWord>
<Heb>מילה ראשונה</Heb>
<Eng>first word</Eng>
</newWord>
</xml>
我试图找到标签<newWord>
,然后转到它的子标签<Eng>yellow</Eng>
如果我通过CCD_ 3找到它,我应该需要去屏蔽它并删除元素CCD_。
我试着用下面的代码来做,但我不知道该怎么去<newWord>
的子代。许多感谢你的帮助。
这是我的代码:
<?php
$del=true;
if ($del==TRUE){
$searchString = 'yellow';
header('Content-type: text/xml; charset=utf-8');
$xml = simplexml_load_file('./Dictionary_user.xml');
foreach($xml->children() as $child){
if($child->getName() == "newWord") {
if($searchString == $child['Eng']) {
$dom->parentNode->removeChild($xml);
} else {
echo('no match found resualt');
}
}
}
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->formatOutput = true;
$dom->load('Dictionary_user.xml');
$dom->save("Dictionary_user.xml");
$dom->saveXML();
header('Location: http://127.0.0.1/www/www1/ajax/ajax4/workwell/popus1.html');
}
?>
试试这个:
$searchString = 'yellow';
$xml = simplexml_load_file('./Dictionary_user.xml');
foreach($xml->children() as $child){
if($child->getName() == "newWord") {
if($child->Eng == $searchString){
$dom = dom_import_simplexml($child);
$dom->parentNode->removeChild($dom);
}
}
}
echo $xml->asXML();
在这条线上
if($searchString == $child['Eng']) {
您正在尝试比较子节点的主体,但它不会自动转换为字符串。它仍然是SimpleXMLElement object
,因此比较失败。
尝试将其显式转换为字符串以获取标记的正文。
if($searchString == (string)$child['Eng']) {