سؤال

Following is my XML file i want to update the doller and cent values which are inside latestBid. I first tried the doller values but it's not working. i even tried to update the description ('//item[id="4"]/description') even that didn't work. Please tell me what i'm doing wrong here. XML file

<?xml version="1.0"?>
<items>
  <item>
    <itemNumber>4</itemNumber>
    <latestBid>
      <latestCustomerId>1</latestCustomerId>
      <bidPrice>
        <doller>2342</doller>
        <cent>23</cent>
      </bidPrice>
    </latestBid>
  </item>
  <item>
    <itemNumber>5</itemNumber>
    <latestBid>
      <latestCustomerId>1</latestCustomerId>
      <bidPrice>
        <doller>35345</doller>
        <cent>78</cent>
      </bidPrice>
    </latestBid>
  </item>
</items>

PHP file

<?php
$url = '../../data/auction2.xml';
$itemNumber ="4";
$bidDoller = 45;
$bidCent=55;
$doc = new DomDocument();
$xml=simplexml_load_file($url);
//echo "came 1";working
foreach ($xml->xpath('//item[@itemNumber="4"]/latestBid/bidPrice/doller') as $desc) {
  echo "came 2";//nt working
  $dom=dom_import_simplexml($desc);
  $dom->nodeValue = $bidDoller; 
  }
file_put_contents($url, $xml->asXML()); 
?>

edited. Still not working

هل كانت مفيدة؟

المحلول

thank you every one for the support by editing and answering I finally did it. since it wasn't easy for me to do this i'm posting the answer to help someone like me :). i didn't change the xml.

php file

$url = '../../data/auction2.xml';

$itemNumber ="4";
$bidDoller = 85;
$bidCent=95;

$xml=simplexml_load_file($url);
$resultDoller= $xml->xpath('//item[itemNumber="'.$itemNumber.'"]/latestBid/bidPrice/doller');
$resultCent= $xml->xpath('//item[itemNumber="'.$itemNumber.'"]/latestBid/bidPrice/cent');

$resultDoller[0][0]=$bidDoller;
$resultCent[0][0]=$bidCent;
print $xml->asXML();

file_put_contents($url, $xml->asXML()); 

نصائح أخرى

Following worked for me, //XML

<?xml version="1.0"?>
<items>
  <item id="4">
    <itemNumber>4</itemNumber>
    <latestBid>
      <latestCustomerId>1</latestCustomerId>
      <bidPrice>
        <doller>2342</doller>
        <cent>23</cent>
      </bidPrice>
    </latestBid>
  </item>
  <item>
    <itemNumber>5</itemNumber>
    <latestBid>
      <latestCustomerId>1</latestCustomerId>
      <bidPrice>
        <doller>35345</doller>
        <cent>78</cent>
      </bidPrice>
    </latestBid>
  </item>
</items>

//PHP

<?php
$url = '../../data/auction2.xml';
$itemNumber ="4";
$bidDoller = 45;
$bidCent=55;
$doc = new DomDocument();
$xml=simplexml_load_file($url);
$result = $xml->xpath('//item[@id="4"]/latestBid/bidPrice/doller');

echo "<pre>";
print_r($result);
//echo "came 1";working
foreach ($xml->xpath('//item[@id="4"]/latestBid') as $desc) {
  echo "came 2";//nt working
  $dom=dom_import_simplexml($desc);
  $dom->nodeValue = $bidDoller; 
  }
//file_put_contents($url, $xml->asXML()); 
?>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top