Hey Guys, I'm pulling my hair out trying to figure out why this isn't work. I have to xml files. Both of them are written exactly the same way except for the values of course. keys.xml <?xml version="1.0" encoding="utf-8"?> <keys> <key id="1"> <id>1</id> <k_value>_Test1</k_value> </key> <key id="2"> <id>2</id> <k_value>_Test2</k_value> </key> <key id="45"> <k_id>45</k_id> <k_value>_MonkeyTest</k_value> </key> </keys> Code (markup): values.xml <?xml version="1.0" encoding="utf-8" ?> <values> <value id="1"> <id>1</id> <k_id>23</k_id> </value> <value id="2"> <id>2</id> <k_id>35</k_id> </value> </values> Code (markup): I am using this code to retrieve the lastChild and get the last id and then insert a new element. I do have another function with the similar code that gets the elements with the different names. $array = array(); $doc = new DOMDocument(); $doc->formatOutput = true; $doc->load("{$this->paths->avs_web}xml/keys.xml"); $keys = $doc->getElementsByTagName("keys"); foreach ($keys as $item) { $lastelement = $item->lastChild; $lastid = (int)$lastelement->getAttribute("id"); $newid = $lastid + 1; $newElement = $doc->createElement("key"); $newElement->setAttribute("id", $newid); $newElement1 = $doc->createElement("k_id", $newid); $newElement2 = $doc->createElement("k_value", $l); $newElement->appendChild($newElement1); $newElement->appendChild($newElement2); $item->appendChild($newElement); } $doc->save("{$this->paths->avs_web}xml/keys.xml"); return $newid; Code (markup): When I read keys.xml lastChild returns DOMElement. But when I read values.xml the lastChild is DOMText. $lastelement = $item->lastChild; Code (markup): The DOMText value is "\n". I'm not sure why that would be because both the files are written exactly the same way. I did remove all the files from values.xml and that made it work, the lastChild was a DOMElement but after file gets saved with the new node then it reverts back to the same problem.