prometheos Posted January 29, 2010 Share Posted January 29, 2010 i want to use simplexml to add a node value in my xml file. here is the layout of my xml: <items> <item quantity="1"> <itemSource>Ireland</itemSource> <itemName>marcus</itemName> <description>marcus gears of war</description> <deliveryCity>me</deliveryCity> <delivery>if you can find him</delivery> <date> <day>day</day> <month>month</month> <year>year</year> </date> <picSource>images/gears-of-war.jpg</picSource> <requested> <by></by> <confirmed></confirmed> </requested> </item> </items Heres how im trying to do it? can it be done this way? $filename = "./xml_files/".$userId.".xml"; $xml = simplexml_load_file($filename); foreach($xml->items->item as $item){ if($item->itemName == $itemN){ $item->requested->by = $userId; } } $xml->asXml()); Quote Link to comment https://forums.phpfreaks.com/topic/190175-using-simplexml-to-edit-a-node-value/ Share on other sites More sharing options...
JAY6390 Posted January 29, 2010 Share Posted January 29, 2010 Something like this? <?php $xml = '<items> <item quantity="1"> <itemSource>Ireland</itemSource> <itemName>marcus</itemName> <description>marcus gears of war</description> <deliveryCity>me</deliveryCity> <delivery>if you can find him</delivery> <date> <day>day</day> <month>month</month> <year>year</year> </date> <picSource>images/gears-of-war.jpg</picSource> <requested> <by></by> <confirmed></confirmed> </requested> </item> </items>'; $dom = new DomDocument(); $dom->loadXML($xml); $items = $dom->getElementsByTagName('items')->item(0); $item = $dom->createElement('item'); $item->setAttribute('quantity', 1); $items->appendChild($item); echo '<pre>'.htmlentities(print_r($dom->saveXML(), true)).'</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/190175-using-simplexml-to-edit-a-node-value/#findComment-1003415 Share on other sites More sharing options...
prometheos Posted January 29, 2010 Author Share Posted January 29, 2010 thanks for the reply! would that just add a new <item> in? don't know much about dom as ive only used simplexml. say i wanted to add a value to the <by> tags, how would i do that? Quote Link to comment https://forums.phpfreaks.com/topic/190175-using-simplexml-to-edit-a-node-value/#findComment-1003615 Share on other sites More sharing options...
salathe Posted January 29, 2010 Share Posted January 29, 2010 Given your posted XML, shouldn't $xml->items->item instead be $xml->item ? Quote Link to comment https://forums.phpfreaks.com/topic/190175-using-simplexml-to-edit-a-node-value/#findComment-1003700 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.