Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output XMLWriter to XML file

Tags:

php

xml

xmlwriter

I am currently using XMLWriter to display an xml file. However I would like to know how I could export the output to a .xml file.

My current code is:

$res = mysql_query($sql);

$xml = new XMLWriter();

$xml->openURI("php://output");
$xml->startDocument();
$xml->startElement('stores');

while ($row = mysql_fetch_assoc($res)) {
//loads of code
}
$xml->endElement();

$xml->flush();
like image 463
max_ Avatar asked Mar 14 '11 18:03

max_


People also ask

How to write XML using XmlWriter in c#?

Xml; var sts = new XmlWriterSettings() { Indent = true, }; using XmlWriter writer = XmlWriter. Create("data. xml", sts); writer. WriteStartDocument(); writer.

What is the difference between XmlWriter and XmlTextWriter?

XmlWriter is an abstract class. XmlTextWriter is a specific implementation of XmlWriter .

What is XML writer?

The XmlWriter class writes XML data to a stream, file, text reader, or string. It supports the W3C Extensible Markup Language (XML) 1.0 (fourth edition) and Namespaces in XML 1.0 (third edition) recommendations.

How read and write data from XML in C#?

The XmlReader, XmlWriter and their derived classes contains methods and properties to read and write XML documents. With the help of the XmlDocument and XmlDataDocument classes, you can read entire document. The Load and Save method of XmlDocument loads a reader or a file and saves document respectively.


1 Answers

Use a filename instead of php://output in the openURI() method.

$writer = new XMLWriter();
$writer->openURI('test.xml');
$writer->startDocument("1.0");
$writer->startElement("greeting");
$writer->text('Hello World');
$writer->endDocument();
$writer->flush();
like image 144
Gordon Avatar answered Sep 20 '22 21:09

Gordon