如何删除节点的文本而不删除子节点?

发布于 2024-09-27 09:18:19 字数 1000 浏览 1 评论 0原文

我正在努力根据数据库中的值创建 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>

我的问题是,我似乎无法找到一种方法来从节点中删除“英语”文本而不擦除里面的子节点titlevalue。这就是当我用 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸵鸟症 2024-10-04 09:18:19

嗯,最简单的方法是将 nodeValue 存储在临时变量中,并在创建其他节点之前取消设置 nodeValue

$lang = $Element->nodeValue;
$Element->nodeValue = "";
$doc2 = $Element->ownerDocument;
$titleElement = $doc2->createElement('title','language');
$valueElement = $doc2->createElement('value', $lang);
$Element->appendChild($titleElement);
$Element->appendChild($valueElement);

但您还应该能够通过

$Element->removeChild($Element->childNodes->item(0));

最后删除 DOMText 节点。

Well, the easiest would be to store the nodeValue in a temporary variable and unset the nodeValue before creating the other nodes.

$lang = $Element->nodeValue;
$Element->nodeValue = "";
$doc2 = $Element->ownerDocument;
$titleElement = $doc2->createElement('title','language');
$valueElement = $doc2->createElement('value', $lang);
$Element->appendChild($titleElement);
$Element->appendChild($valueElement);

But you should also be able to remove the DOMText node via

$Element->removeChild($Element->childNodes->item(0));

at the end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文