Jump to content

using simpleXML to edit a node value


prometheos

Recommended Posts

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());

Link to comment
https://forums.phpfreaks.com/topic/190175-using-simplexml-to-edit-a-node-value/
Share on other sites

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>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.