开发者

How can I remove the text of a node without removing child nodes?

I'm working on creating X开发者_如何转开发ML 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 :)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜