HardCoreMore Posted October 8, 2008 Share Posted October 8, 2008 How can i add attribute at the same time when i am createing new element. This is code for creating new element. $book = simplexml_load_file('user.xml'); $dom = dom_import_simplexml($book); $dom->appendChild(new DOMElement("user", "New Value")); How can i add attribute to this new user element when i create it? This is xml <?xml version="1.0"?> <root> <user id="34"> <pic>value</pic> <pic>value</pic> <pic>value</pic> </user> <userid="45"> <pic>value</pic> <pic>value</pic> <pic>value</pic> </user> <user id="22"> <pic>value</pic> <pic>value</pic> <pic>value</pic> </user> </root> Please help i searched the web but i really can't find it. Link to comment https://forums.phpfreaks.com/topic/127560-add-attribute-to-the-element-in-xml/ Share on other sites More sharing options...
HardCoreMore Posted October 8, 2008 Author Share Posted October 8, 2008 Or if you can tell me how to can i determine last child of user node with xml that would be of great help. Link to comment https://forums.phpfreaks.com/topic/127560-add-attribute-to-the-element-in-xml/#findComment-659987 Share on other sites More sharing options...
imzeeshan Posted October 8, 2008 Share Posted October 8, 2008 Why not use just simplexml only . specifically SimpleXMLElement->addAttribute DomDocument is too cumbersome. Link to comment https://forums.phpfreaks.com/topic/127560-add-attribute-to-the-element-in-xml/#findComment-659994 Share on other sites More sharing options...
HardCoreMore Posted October 8, 2008 Author Share Posted October 8, 2008 Hey i figure it out i import simple xml into dom with dom_import_simplexml($book); and dom into simple xml with simplexml_import_dom($book); I needed dom cauz i didn't know how to determine last child with simple xml. Link to comment https://forums.phpfreaks.com/topic/127560-add-attribute-to-the-element-in-xml/#findComment-660018 Share on other sites More sharing options...
imzeeshan Posted October 13, 2008 Share Posted October 13, 2008 Determining the last child with simple xml is simple again. Use a for loop or for each after the xpath result. Link to comment https://forums.phpfreaks.com/topic/127560-add-attribute-to-the-element-in-xml/#findComment-663847 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.