views:

17

answers:

1

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 :)

Thanks for your help!

A: 

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.

Gordon
Chris
OK now the secondary problem has become a bigger one than I thought ... renameNode() doesn't exist in DOM 2 ("not yet implemented") so I'll start another question with that.
Chris