Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to edit an XML document with a dynamic variable from PHP
    primarykey
    data
    text
    <p>I am trying to edit an XML document using PHP, but it does not seem to work, wonder where I am going wrong!</p> <p>I am trying to do it the following way which I found online. Can anyone please show me the right direction?</p> <p><strong>XML</strong></p> <pre><code>&lt;gold&gt; &lt;coin&gt; &lt;id&gt;1&lt;/id&gt; &lt;title&gt;Gold Coin 50 Grams&lt;/title&gt; &lt;price&gt;500 rupees&lt;/price&gt; &lt;dimension&gt;20 MM&lt;/dimension&gt; &lt;thumb_url&gt;http://animsinc.com/Expertise-media.png&lt;/thumb_url&gt; &lt;/coin&gt; &lt;coin&gt; &lt;id&gt;2&lt;/id&gt; &lt;title&gt;Gold Coin 50 Grams&lt;/title&gt; &lt;price&gt;500 rupees&lt;/price&gt; &lt;dimension&gt;20 MM&lt;/dimension&gt; &lt;thumb_url&gt; http://animsinc.com/Expertise-media.png&lt;/thumb_url&gt; &lt;/coin&gt; ... &lt;/gold&gt; </code></pre> <p><strong>PHP Code</strong></p> <pre><code>$xmlDoc = new DOMDocument(); $xmlDoc-&gt;loadXml($str); $events = $xmlDoc-&gt;getElementsByTagName("coin"); foreach($events as $event){ $eventNames = $event-&gt;getElementsByTagName("price"); $eventN = $eventNames-&gt;item(0)-&gt;nodeValue; if('500' == $eventN){ // ** Failed here, instead of '500', // I used '500 rupees' and it worked, as that matches the original text.** $eventNames-&gt;item(0)-&gt;nodeValue = $rest ; } } var_dump($xmlDoc-&gt;saveXML()); </code></pre> <p>Here's the desired result. Notice how I've changed the price tag to 2495 rupees. </p> <pre><code> &lt;gold&gt; &lt;coin&gt; &lt;id&gt;1&lt;/id&gt; &lt;title&gt;Gold Coin 50 Grams&lt;/title&gt; &lt;price&gt;2495 rupees&lt;/price&gt; &lt;dimension&gt;20 MM&lt;/dimension&gt; &lt;thumb_url&gt;http://animsinc.com/Expertise-media.png&lt;/thumb_url&gt; &lt;/coin&gt; &lt;coin&gt; &lt;id&gt;2&lt;/id&gt; &lt;title&gt;Gold Coin 50 Grams&lt;/title&gt; &lt;price&gt;2495 rupees&lt;/price&gt; &lt;dimension&gt;20 MM&lt;/dimension&gt; &lt;thumb_url&gt; http://animsinc.com/Expertise-media.png&lt;/thumb_url&gt; &lt;/coin&gt; ... &lt;/gold&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload