Php 如何从xml文件中删除特定元素
我想删除:Php 如何从xml文件中删除特定元素,php,xml,Php,Xml,我想删除: <newWord> <Heb>צהוב</Heb> <Eng>yellow</Eng> </newWord> 在这条线上 if($searchString == $child['Eng']) { 您试图比较子节点的主体,但它不会自动转换为字符串。它仍然是一个simplexmlement对象,因此比较失败 尝试显式地将其转换为字符串以获取标记体 if($searchString == (str
<newWord>
<Heb>צהוב</Heb>
<Eng>yellow</Eng>
</newWord>
在这条线上
if($searchString == $child['Eng']) {
您试图比较子节点的主体,但它不会自动转换为字符串。它仍然是一个simplexmlement对象
,因此比较失败
尝试显式地将其转换为字符串以获取标记体
if($searchString == (string)$child['Eng']) {
试试这个:
$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']) {
if($searchString == (string)$child['Eng']) {
$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();