//加载指定xmls
$xmldoc->load($pathtou.$dirfile); //xml 文件
$xmldoc -> formatOutput = true;
$stus =$xmldoc->getElementsByTagName("message");
$stu_nodes =$xmldoc->getElementsByTagName("message");
的值
//echo $stu_nodes->length;
$tssave=false;
for($i=0;$i<$stu_nodes->length;$i++)
{
var_dump($i);
$source = $stus->item($i)->getElementsByTagName("source")->item(0)->nodeValue;
$translatorcomment = $stus->item($i)->getElementsByTagName("translatorcomment")->item(0)->nodeValue;
var_dump($translatorcomment);//为null 节点不存在
for($comnum = 0;$comnum<count($sourcejike);$comnum++)
{
// $dataDom = $stus->item($i)->getElementsByTagName('translatorcomment');//判断节点存在否
// if($dataDom ->length == 0)
// {echo "nothing";
// }
if($source==$sourcejike[$comnum])
{
if($translatorcomment!=null)
{
//写值为NE
$stus =$xmldoc->getElementsByTagName("message");
$stu1=$stus->item($i);
$stu1_age = $stu1->getElementsByTagName("translatorcomment")->item(0);
$stu1_age->nodeValue = 'NE';
}
if($translatorcomment==null)
{
$b_node =$xmldoc->getElementsByTagName("message");
$d_node = $xmldoc->createElement('translatorcomment');//创建translatorcomment元素节点
$d_node_content =$xmldoc->createTextNode ('NE');//节点值
$d_node-> appendChild($d_node_content);//在translatorcomment节点元素中添加文本元素节点
$b_node->item($i)->appendChild($d_node);//在所有名字为translatorcomment的元素节点数组中的第一个元素节点中添加translatorcomment元素节点
}
}
}
}
$xmldoc->save($pathtou.$dirfile);//保存
}
PHP document读写xml 增加节点和改节点值
最新推荐文章于 2021-04-03 19:12:56 发布