Jump to content

DOMDocument::createElement fatal error (undefined method)


freelance84

Recommended Posts

NB:

The first section of php in this thread is extracted from the second. It is where the problem lies - ($newItem = $XMLpage->createElement('item', $updateValue);).

 

 

I am trying to simply add an element into an xml document via DOMDocument::createElement. However the following returns a fatal error:

<b>Fatal error</b>:  Call to undefined method DOMElement::createElement()

The error section in question extracted from the full function

//load up and get ready the xml file to edit
$xml = new DOMDocument('1.0', 'utf-8');
$xml->load($fullPathToXML);

//load the page we are changing
$XMLpage = $xml->getElementsByTagName('page')->item($page_number);

//created the new item node
$newItem = $XMLpage->createElement('item', $updateValue);

//append the item to the xml sheet on the correct page
$XMLpage->appendChild($newItem);

//now save the xml to file					
$xml->save($fullPathToXML);

 

 

Would anyone have any ideas why it is returning a fatal error here? I have used the official example on the DOMDocument::createElement and the php version i am using is 5.3.8

 

 

The full function (the rest of which works fine):

/*** modifying a single section of an xml sheet ***/
public function model_updateXMLfile($id = null, $updateValue = null, $inputType = null)
	{
		//if either the values sent are empty
		if(empty($id))
			{
				exit();
			}

		//get the comain name form the sessions
		$xmlFile= $_SESSION['xmlFile'];
		//replace all . with DOT as this will be the actual file name
		$xmalFileName = str_replace('.','DOT',$xmlFile);
		//get the xmla location directory from the sessions
		$xmlLocation = $_SESSION['xml_location'];
		//first make the path a whole path based the $_SESSION['xml_location']
		$fullPathToXML = WEBROOT.'xml/'.$xmlLocation.'/'.$xmalFileName.'.xml';


		//now get the coords of what page to change
		$id_exploded = explode('_',$id);

		//page number to edit
		$page_number = $id_exploded[0];

		//item number
		$item_number = $id_exploded[1];

		//load up and get ready the xml file to edit
		$xml = new DOMDocument('1.0', 'utf-8');
		$xml->formatOutput = true;
		$xml->preserveWhiteSpace = false;
		$xml->load($fullPathToXML);

		//load the page we are changing
		$XMLpage = $xml->getElementsByTagName('page')->item($page_number);

		//if the user wants to change the top title
		if($item_number == 'topTitle')
			{
				$htmlTitle = $XMLpage->getElementsByTagName('topTitle')->item(0);
				$htmlTitle ->nodeValue = $updateValue;
				$XMLpage->replaceChild($htmlTitle, $htmlTitle);
				$xml->save($fullPathToXML);
			}

		//else changing an item
		else{
				/**check the item exists, if it does then simply edit ***/
				if($XMLpage->getElementsByTagName('item')->item($item_number))
					{
						//update the content of the xml tag
						$xmlItem = $XMLpage->getElementsByTagName('item')->item($item_number);

						//change the content of the xml tag
						$xmlItem ->nodeValue = $updateValue;

						//update the node in the xml file
						$XMLpage->replaceChild($xmlItem, $xmlItem);

						//now change the attibute of the item according to what was sent
						$xmlItem->setAttribute('type', "$inputType");

						//now save the xml to file					
						$xml->save($fullPathToXML);
					}
				/** a new item is being created **/
				else
					{
						//created the new item node
						$newItem = $XMLpage->createElement('item', $updateValue);

						//append the item to the xml sheet on the correct page
						$XMLpage->appendChild($newItem);

						//now save the xml to file					
						$xml->save($fullPathToXML);
					}
			}

		//return the message saved
		return 'saved';
	}

Link to comment
Share on other sites

THIS FAILED

//load up and get ready the xml file to edit
$xml = new DOMDocument('1.0', 'utf-8');
$xml->load($fullPathToXML);

//load the page we are changing
$XMLpage = $xml->getElementsByTagName('page')->item($page_number);

//created the new item node
$newItem = $XMLpage->createElement('item', $updateValue);

//append the item to the xml sheet on the correct page
$XMLpage->appendChild($newItem);

//now save the xml to file					
$xml->save($fullPathToXML);

 

THIS SUCCEEDED

//load up and get ready the xml file to edit
$xml = new DOMDocument('1.0', 'utf-8');
$xml->load($fullPathToXML);

//load the page we are changing
$XMLpage = $xml->getElementsByTagName('page')->item($page_number);

//create and append the new item in on go as according to the manual from the this forum by 'Daniel'
$XMLpage->appendChild($xml->createElement('item', 'test content'));

//now save the xml to file					
$xml->save($fullPathToXML);

 

I'm really not sure how the top version kept returning with a message saying the method createElement doesn't exist when the bottom version works...  :shrug:

 

But it now works either way.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.