0

I have a shopping catalog with a button at the end of the each row to add the item to a shopping cart.i need to increment the "Quantity on hold(IqtyOH)" by one each time the user press the add button.I need to modify the xml document according to that.

here is my xml file just to get and idea.

<items>
 <item>
  <IId>1</IId>
  <Iname>samsung</Iname>
  <Iprice>360</Iprice>
  <IqtyAv>9</IqtyAv>
  <IqtyOH>0</IqtyOH>
  <IqtySold>0</IqtySold>
  <ItemDesc>Galaxy</ItemDesc>
 </item>
 <item>
  <IId>2</IId>
  <Iname>sony</Iname>
  <Iprice>1200</Iprice>
  <IqtyAv>12</IqtyAv>    
  <IqtyOH>0</IqtyOH>
  <IqtySold>0</IqtySold>
  <ItemDesc>vaio</ItemDesc>
 </item>
</items>

I m trying to use xpath to retrieve the information from the xml file but its no use.Something is wrong. This is the code that I'm working on.

$dom = new DOMDocument;
$dom->loadXML(file_get_contents($xmlFile));

$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//item[IID='$IId']/IqtyOH");
$node  = $nodes->item(0)->nodeValue;
$node++;
$node->nodeValue = $node;
$dom->saveXML();
hakre
  • 193,403
  • 52
  • 435
  • 836
koshi18
  • 53
  • 9
  • You need to enable error reporting - it's just a little mistake - see here: http://stackoverflow.com/a/14504459/367456 and for error reference see here: http://stackoverflow.com/q/12769982/367456 – hakre Nov 02 '13 at 10:45

1 Answers1

2

I think you've got two small errors in your code. In the XPATH

$nodes = $xpath->query("//item[IID='$IId']/IqtyOH");

The IID element should be IId. So you get:

$nodes = $xpath->query("//item[IId='$IId']/IqtyOH");

Secondly $node is the value of the node you found, not a reference to it, so you can't call nodeValue on it. Instead you can do this:

$node = $nodes->item(0)->nodeValue;
$node++;
$nodes->item(0)->nodeValue =$node;
Clart Tent
  • 1,319
  • 2
  • 9
  • 11
  • I did the changes but still it doesnt modify the xml.Do not get any errors either. – koshi18 Nov 02 '13 at 09:13
  • Are you trying to change the content of $xmlFile (that is, save the result back into that file)? If so you probably want to use `$dom->save($xmlFile);`. `$dom->saveXML();` just returns the generated xml. (So if you do `echo $dom->saveXML();` you'll see the updated version.) – Clart Tent Nov 02 '13 at 09:19
  • Thanks alot.changed the code to $dom->save($xmlFile).Now it works.Many thanks. – koshi18 Nov 02 '13 at 10:26