Claim your Biolink Click Here
0 like 0 dislike
1.2k views
in Education & Reference by | 1.2k views

1 Answer

0 like 0 dislike
<?php

mysql_connect('localhost', 'root', 'admin');
mysql_select_db('xml');

$sql = "SELECT udid, country,code FROM country ORDER BY udid";
$res = mysql_query($sql);

$xml = new XMLWriter();

$xml->openURI("php://output");
$xml->startDocument();
$xml->setIndent(true);

$xml->startElement('countries');

while ($row = mysql_fetch_assoc($res)) {

   $xml->startElement("udid");
   $xml->writeRaw($row['udid']);
   $xml->endElement();
 
 
   $xml->startElement("country");
   $xml->writeAttribute('udid', $row['udid']);
   $xml->writeRaw($row['country']);
   $xml->endElement();
 
  $xml->startElement("code");
  $xml->writeRaw($row['code']);
  $xml->endElement();
 
}

$xml->endElement();

header('Content-type: text/xml');
$xml->flush();

?>
by
0 0
Working Perfectly.But Instead of showing XML in browser I wanted to save this XML output as XML file.Also if any attributes are empty it should not be shown.Kindly give
idea/tips.


Aston Martin DB9
450
2004

Bugatti Veyron EB 16.4
1001
2006

Koenigsegg CCX
806
2006

Lamborghini Murcielago LP640
633
2006

Pagani Zonda C12 F
602
2005
0 0
Simply do fwrite like below

$my_file = 'file.xml';
$handle = fopen($my_file, 'a') or die('Cannot open file: '.$my_file);
$data = 'New data line 1';
fwrite($handle, $data);
$new_data = "\n".'New data line 2';
fwrite($handle, $new_data);

Related questions

1 like 0 dislike
1 answer
2 like 0 dislike
3 answers
0 like 0 dislike
2 answers
asked Jun 27, 2019 in Education & Reference by Simmi (820 points) | 278 views
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
3 answers
asked Jan 20, 2014 in Education & Reference by Sam (1.6k points) | 1.9k views
1 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer

Where your donation goes
Technology: We will utilize your donation for development, server maintenance and bandwidth management, etc for our site.

Employee and Projects: We have only 15 employees. They are involved in a wide sort of project works. Your valuable donation will definitely boost their work efficiency.

How can I earn points?
Awarded a Best Answer 10 points
Answer questions 10 points
Asking Question -20 points

1,310 questions
1,471 answers
569 comments
4,809 users