write to xml file

liunx

Guest
hi every one

i have a proble writing to xml file , i have the following code


$file_handle = fopen('filename.xml','a');
$content=('<?xml version="1.0" ?>
<results>
<result>
<mark>data</mark>
</result>
</results>');
fwrite($file_handle,$content);
fclose($file_handle);


the above code works ok on itself but when i come from a different page to the page contains the above code this cases to write the xml file twice

how can i solve this problemfopening a file with 'a' causes the data written to be appended to the end of what is already there.thanks for the answer


i found that whenever i delete the svg heading from the following code , the writting to file would give the result i wanted
but when i add the (five lines after <?php)which contains svg the writting to file will be copied twice , not becouse i appended with a the other data , the reasone i included a becouse i need to add data to the end of the file latter on

can you see what is going on

thanks










<?php

header("Content-type: image/svg+xml");
print('<?xml version="1.0" encoding="iso-8859-1"?>');
print('<svg viewBox="0 0 700 500">');
print('<text x="50" y="50">somthing</text>');
print('<svg>');




include ('Driving-Test.php');
$xml = simplexml_load_string($xmlstr);
$result = $xml->xpath("//answer");
foreach ($result as $row)
$file_handle = fopen('R.xml','a');
$content=('<?xml version="1.0" ?>
<results>
<result>'.$row->mark.'</result>
</results>');
fwrite($file_handle,$content);
fclose($file_handle);

}
?>foreach ($result as $row)
$file_handle = fopen('R.xml','a'); In other words for every row in the result, you reopen the XML file for appending (and that's all you do before going on to the next row, funnily enough).

Of course having anything else in the file before or after
<?xml version="1.0" ?>
<results>
<result>42</result>
</results>would render it invalid as an XML file - it would no longer be well-formed.

You don't mean something more like

<?php
$file_handle = fopen('R.xml','a');
$content = '<?xml version="1.0"?>
<results>';
foreach ($result as $row)
{
$content.="\n<result>".$row->mark."</result>\n";
}
$content .="</results>";
fwrite($file_handle,$content);
fclose($file_handle);
?>Perhaps?


This isn't really a PHP5 problem, as PHP4 would behave the same way (so I guess this isn't a PHP4-PHP5 migration problem of the sort this forum was created for).thanks

and i am sorry , becouse i thought the problerm was related to php5
any way the problem has not been solved yet , i took your solution was OK but when added svg elements to the page , the writting to the file been copied twice

the code as following:

thanks for the help


<?php

header("Content-type: image/svg+xml");
print('<?xml version="1.0" encoding="iso-8859-1"?>');
print('<svg viewBox="0 0 700 500">');
print('<text x="50" y="60">fff</text>');


print('</svg>');


include ('Driving-Test.php');
$xml = simplexml_load_string($xmlstr);
$result = $xml->xpath("//answer");

$file_handle = fopen('R.xml','a');
$content = '<?xml version="1.0"?>
<results>';
foreach ($result as $row)
{
$content.="\n<result>".$row->mark."</result>\n";
}
$content .="</results>";
fwrite($file_handle,$content);
fclose($file_handle);
?>
 
Back
Top