freelance84 Posted December 21, 2011 Share Posted December 21, 2011 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 https://forums.phpfreaks.com/topic/253616-domdocumentcreateelement-fatal-error-undefined-method/ Share on other sites More sharing options...
freelance84 Posted December 21, 2011 Author Share Posted December 21, 2011 I'm really confused. I can't find anything on this. Is there an extra php extension i need to install to be able to use createElement? Link to comment https://forums.phpfreaks.com/topic/253616-domdocumentcreateelement-fatal-error-undefined-method/#findComment-1300155 Share on other sites More sharing options...
freelance84 Posted December 21, 2011 Author Share Posted December 21, 2011 http://www.phpfreaks.com/tutorial/handling-xml-data Link to comment https://forums.phpfreaks.com/topic/253616-domdocumentcreateelement-fatal-error-undefined-method/#findComment-1300161 Share on other sites More sharing options...
freelance84 Posted December 21, 2011 Author Share Posted December 21, 2011 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... But it now works either way. Link to comment https://forums.phpfreaks.com/topic/253616-domdocumentcreateelement-fatal-error-undefined-method/#findComment-1300169 Share on other sites More sharing options...
kicken Posted December 21, 2011 Share Posted December 21, 2011 Top version: $newItem = $XMLpage->createElement('item', $updateValue); Bottom version $xml->createElement('item', 'test content'); XMLPage is not a domdocument, it's a DOMElement. DOMElement has no createElement method. Link to comment https://forums.phpfreaks.com/topic/253616-domdocumentcreateelement-fatal-error-undefined-method/#findComment-1300210 Share on other sites More sharing options...
freelance84 Posted December 21, 2011 Author Share Posted December 21, 2011 Ahh... of course! I completely missed that Thanks! Link to comment https://forums.phpfreaks.com/topic/253616-domdocumentcreateelement-fatal-error-undefined-method/#findComment-1300213 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.