Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从xml文件中删除特定元素_Php_Xml - Fatal编程技术网

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();