|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-12-09 16:53 UTC] info at ferenczigabor dot com
Description:
------------
I unwittingly instantiated an XML object twice, and I expereienced peculiar behavior.
(I indicate the irrelevant lines in the description with "..." sign.
The mentioned XML file contains <addresses> elements, and each element contain a <sent> item:
...
<dataroot>
<asdresses>
...
<address/>
<sent/>
...
</addresses>
<asdresses>
...
<address/>
<sent/>
...
</addresses>
</dataroot>
...).
The PHP version: PHP Version 5.2.4-2ubuntu5.27
I have no chance to upgrade the PHP, because my page is on a provider's server, which runs this version.
<?php
...
...
$dom = new DOMDocument('1.0', 'iso-8859-2');
...
$dom->load($xml_file);
...
...
...
$dom = new DOMDocument('1.0', 'iso-8859-2');//this is the redeclaration
...
$dom->load($xml_file);
...
...
$address = $dom->getElementsByTagName('addresses');
//Later I filled the "sent" item with the number of sent items:
...
while ($i < $i_counter) ) {
...
$akt_item = $address->item($i);//$i is the actual node in the XML file.
...
$sent_number++; // the actual naumber of the sent items for the <address>
$akt_item->getElementsByTagName('sent')->item(0)->nodeValue = $sent_number;
//for debug reason I printed the new value of the node:
echo $akt_item->getElementsByTagName('sent')->item(0)->nodeValue;
//the printed number was the expected new value, so that $sent_number
...
//Finally I tried to print the changed XML file:
echo $dom->saveXML();
...
?>
The last echo prompt printed out the unchanged XML data! So that all the <sent> elements' value remained NULL!
(After I had removed the unwanted redeclaration, the code functioned properly).
This is a bug. Why?
Whenever I declare a variable:
$x = "Some value";
...
then I use this variable again:
$x = "Other value";
From the last decalaration line $x's value strictly will be "Other value".
But my described code used sometime the first instance of the "$dom" (in the "saveXML" method), sometime the second instance (in the "...->nodeValue = ..." and the "echo ...nodeValue"); or v.s.
I guess, the good solution should be to overload the older declaration with the newest one, or to arise an error.
Gabor Ferenczi
Test script:
---------------
<?php
...
...
$dom = new DOMDocument('1.0', 'iso-8859-2');
...
$dom->load($xml_file);
...
...
...
$dom = new DOMDocument('1.0', 'iso-8859-2');//this is the redeclaration
...
$dom->load($xml_file);
...
...
$address = $dom->getElementsByTagName('addresses');
//Later I filled the "sent" item with the number of sent items:
...
while ($i < $i_counter) ) {
...
$akt_item = $address->item($i);//$i is the actual node in the XML file.
...
$sent_number++; // the actual naumber of the sent items for the <address>
$akt_item->getElementsByTagName('sent')->item(0)->nodeValue = $sent_number;
//for debug reason I printed the new value of the node:
echo $akt_item->getElementsByTagName('sent')->item(0)->nodeValue;
//the printed number was the expected new value, so that $sent_number
...
//Finally I tried to print the changed XML file:
echo $dom->saveXML();
...
?>
Patchesfile_xml (last revision 2013-12-10 11:17 UTC by info at ferenczigabor dot com)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 14:00:01 2025 UTC |
Thank you for this bug report. To properly diagnose the problem, we need a short but complete example script to be able to reproduce this bug ourselves. A proper reproducing script starts with <?php and ends with ?>, is max. 10-20 lines long and does not require any external resources such as databases, etc. If the script requires a database to demonstrate the issue, please make sure it creates all necessary tables, stored procedures etc. Please avoid embedding huge scripts into the report. -- I get the correct (expected) behavior using <?php $xml = <<<XML <root><node1><node2>empty</node2></node1></root> XML; $dom = new DOMDocument("1.0", "iso-8859-2"); $dom->loadXML($xml); $dom = new DOMDocument("1.0", "iso-8859-2"); $dom->loadXML($xml); $node1 = $dom->getElementsByTagName("node1")->item(0); $node1->getElementsByTagName("node2")->item(0)->nodeValue = "full"; echo $dom->saveXML(); // <root><node1><node2>full</node2></node1></root> ?><?php //the entire code's language is Hungarian, please excuse me for the funny mixture of the English and Hungarian define("max_mail",10); define("CrLf","\r\n"); define("Cr","\n"); //link for the xml file: http://paskal-bt.hu/bakny $xml_file = "http://paskal-bt.hu/bakny/PHP-cimlista-proba.xml"; $xml_sablon = "http://paskal-bt.hu/bakny/PHP-xml_sablon.php"; $dom = new DOMDocument('1.0', 'iso-8859-2'); if (!file_exists($xml_file)) { $dom->loadXML($xml_sablon); $dom->save($xml_file); } //if f_exists else { $dom->load($xml_file); } //else $cimlista = $dom->getElementsByTagName('Cimlista'); $napi_mail_szam = 0; $dom = new DOMDocument('1.0', 'ISO-8859-2'); //this is the second instanciateting of the $dom object if (!$dom->load($xml_file)) { echo "Error while parsing the document: $xml_file\n"; exit; } $dom->formatOutput = true; $items = $dom->getElementsByTagName('E-mail'); for ($i=0; $i < $items->length ; $i++) { $mail_items_tomb[$i] = $items->item($i)->nodeValue; }//for i $items = $dom->getElementsByTagName('Elkuldve'); for ($i=0; $i < $items->length ; $i++) { $elkuldve_items_tomb[$i] = $items->item($i)->nodeValue; }//for i $i = 0; $tomb_meret = count($mail_items_tomb); $elkuldve_valasz = "1"; while ($i < $tomb_meret) { $akt_elem = $cimlista->item($i); $siker = true; //in the entire PHP code $siker is the result of a mail() function. For the 20 lines maximum code-size I deleted all the lines, which are unrelevants. if ($siker) { $akt_elem->getElementsByTagName('Elkuldve')->item(0)->nodeValue = $elkuldve_valasz; echo $akt_elem->getElementsByTagName('Elkuldve')->item(0)->nodeValue; //this line is not in the entire code, I inserted only for demonstrating the bug. } //if siker ++$i; } //while vége $dom->saveXML();//this line is not in the entire code, I inserted only for demonstrating the bug. $dom->normalize; //$dom->save($xml_file); //the entire code save the new xml database file here. I commented out this line only for the PHP demonstration. echo "Az eredmény mentve"; ?>