Changing an XML node value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
The Omen
Forum Newbie
Posts: 8
Joined: Sat Apr 04, 2009 9:45 am

Changing an XML node value

Post by The Omen »

I have a page that needs to change the text within an XML node. Currently I'm using this:

Code: Select all

$docpagedesc = stripslashes($_POST['docpagedesc']);
 
$main_text = $xml->getElementsBytagName('maintext')->item(0)->firstChild;
$main_text->nodeValue = $docpagedesc;
That works fine if there is already text present within the node. For example:

Code: Select all

<maintext>Hello World</maintext>
The problem occurs if the node is blank - if it is, the text doesn't get added. For example:

Code: Select all

<maintext />
Anyone know of a way round this?
Post Reply