如何删除节点的文本而不删除子节点?
我正在努力根据数据库中的值创建 XML 文档。最初,程序导出此 XML:
<customDataElementlanguage>English</customDataElementlanguage>
我创建了此 PHP 来将 XML 树:更改
if ($Element->nodeValue = "EN") { $Element->nodeValue = "English"; }
$doc2 = $Element->ownerDocument;
$titleElement = $doc2->createElement('title','language');
$valueElement = $doc2->createElement('value',$Element->nodeValue);
$Element->appendChild($titleElement);
$Element->appendChild($valueElement);
//$Element->nodeValue="";
为:
<customDataElementlanguage>
English
<title>language</title>
<value>English</value>
</customDataElementlanguage>
我的问题是,我似乎无法找到一种方法来从节点中删除“英语”文本而不擦除里面的子节点title
和value
。这就是当我用 $Element->nodeValue="";
结束 PHP 代码时发生的情况
我还想将 customDataElementlanguage 节点的名称更改为 customDataElement 但我可以稍后再处理认为 :)
I'm working on creating XML docs from values in a database. Initially, the program exports this XML:
<customDataElementlanguage>English</customDataElementlanguage>
I've created this PHP to change the XML tree:
if ($Element->nodeValue = "EN") { $Element->nodeValue = "English"; }
$doc2 = $Element->ownerDocument;
$titleElement = $doc2->createElement('title','language');
$valueElement = $doc2->createElement('value',$Element->nodeValue);
$Element->appendChild($titleElement);
$Element->appendChild($valueElement);
//$Element->nodeValue="";
into this:
<customDataElementlanguage>
English
<title>language</title>
<value>English</value>
</customDataElementlanguage>
My problem is that I can't seem to find a way to remove the "English" text from the node without wiping out the child nodes title
and value
inside. That's what happens when I end my PHP code with $Element->nodeValue="";
I'd also like to change the name of the customDataElemementlanguage node to customDataElement but I can work on that later I suppose :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,最简单的方法是将
nodeValue
存储在临时变量中,并在创建其他节点之前取消设置nodeValue
。但您还应该能够通过
最后删除 DOMText 节点。
Well, the easiest would be to store the
nodeValue
in a temporary variable and unset thenodeValue
before creating the other nodes.But you should also be able to remove the
DOMText
node viaat the end.