If you want your XML-output to be seen as XML by the browser, you need to modify your header. The XmlWriter does not do this for you! Therefore, the first line of your script should be:
header("Content-type: text/xml");
Fonctions XMLWriter
Classes pré-définies
XMLWriter
Méthodes
-
XMLWriter::endAttribute : Attribut de fin
-
XMLWriter::endCData : Termine une section CDATA
-
XMLWriter::endComment : Crée la fin d'un commentaire
-
XMLWriter::endDocument : Termine le document courant
-
XMLWriter::endDTDAttlist : Termine une AttList de la DTD courante
-
XMLWriter::endDTDElement : Termine l'élément courant de la DTD
-
XMLWriter::endDTDEntity : Termine l'entité courante de la DTD
-
XMLWriter::endDTD : Termine la DTD courante
-
XMLWriter::endElement : Termine l'élément courant
-
XMLWriter::endPI : Termine le PI courant
-
XMLWriter::flush : Affiche le buffer courant
-
XMLWriter::fullEndElement : Termine l'élément courant
-
XMLWriter::openMemory : Crée un nouvel objet xmlwriter en utilisant la mémoire pour l'affichage des chaînes
-
XMLWriter::openURI : Crée un nouvel objet xmlwriter en utilisant l'URI source pour l'affichage
-
XMLWriter::outputMemory : Retourne le buffer courant
-
XMLWriter::setIndentString : Définit une chaîne qui sera utilisée pour l'indentation
-
XMLWriter::setIndent : Active ou désactive l'indentation
-
XMLWriter::startAttributeNs : Crée un attribut d'espace de noms
-
XMLWriter::startAttribute : Crée un attribut de départ
-
XMLWriter::startCData : Commence une section CDATA
-
XMLWriter::startComment : Comment un commentaire
-
XMLWriter::startDocument : Commence un document
-
XMLWriter::startDTDAttlist : Commence une AttList d'une DTD
-
XMLWriter::startDTDElement : Commence un élément de DTD
-
XMLWriter::startDTDEntity : Commence une entité d'une DTD
-
XMLWriter::startDTD : Commence une DTD
-
XMLWriter::startElementNs : Commence un élément d'espace de noms
-
XMLWriter::startElement : Commence un élément
-
XMLWriter::startPI : Commence un PI
-
XMLWriter::text : Écrit du texte
-
XMLWriter::writeAttributeNs : Écrit un attribut d'un espace de noms
-
XMLWriter::writeAttribute : Écrit un attribut
-
XMLWriter::writeCData : Écrit une balise CDATA
-
XMLWriter::writeComment : Écrit une balise de commentaire
-
XMLWriter::writeDTDAttlist : Écrit une balise AttList d'une DTD
-
XMLWriter::writeDTDElement : Écrit un élément d'une DTD
-
XMLWriter::writeDTDEntity : Écrit une entité d'une DTD
-
XMLWriter::writeDTD : Écrit une balise DTD
-
XMLWriter::writeElementNS : Écrit un élément d'un espace de noms
-
XMLWriter::writeElement : Écrit un élément
-
XMLWriter::writePI : Écrit un PI
-
XMLWriter::writeRaw : Écrit un texte XML brut
Sommaire
- XMLWriter::endAttribute — Termine un attribut
- XMLWriter::endCData — Termine un bloc CDATA
- XMLWriter::endComment — Termine un commentaire
- XMLWriter::endDocument — Termine un document
- XMLWriter::endDTDAttlist — Termine la liste des attributs de la DTD courante
- XMLWriter::endDTDElement — Termine l'élément de la DTD courante
- XMLWriter::endDTDEntity — Termine l'entité de la DTD courante
- XMLWriter::endDTD — Termine la DTD courante
- XMLWriter::endElement — Termine l'élément courant
- XMLWriter::endPI — Termine le PI courant
- XMLWriter::flush — Affiche le buffer courant
- XMLWriter::fullEndElement — Termine l'élément courant
- XMLWriter::openMemory — Crée un nouveau xmlwriter en utilisant la mémoire pour l'affichage des chaînes
- XMLWriter::openURI — Crée un nouveau xmlwriter, en utilisant l'URI source pour l'affichage
- XMLWriter::outputMemory — Retourne le buffer courant
- XMLWriter::setIndentString — Définit la chaîne à utiliser pour l'indentation
- XMLWriter::setIndent — Active ou non l'indentation
- XMLWriter::startAttributeNs — Crée un attribut pour l'espace de noms
- XMLWriter::startAttribute — Crée un attribut
- XMLWriter::startCData — Crée une balise CDATA
- XMLWriter::startComment — Crée un commentaire
- XMLWriter::startDocument — Crée un document
- XMLWriter::startDTDAttlist — Crée une liste d'attributs pour la DTD
- XMLWriter::startDTDElement — Crée un élément DTD
- XMLWriter::startDTDEntity — Crée une entité DTD
- XMLWriter::startDTD — Crée une DTD
- XMLWriter::startElementNs — Crée un élément d'un espace de noms
- XMLWriter::startElement — Crée un élément
- XMLWriter::startPI — Crée une balise PI
- XMLWriter::text — Écrit du texte
- XMLWriter::writeAttributeNs — Écrit un attribut d'un espace de noms
- XMLWriter::writeAttribute — Écrit un attribut
- XMLWriter::writeCData — Écrit un bloc CDATA
- XMLWriter::writeComment — Écrit un commentaire
- XMLWriter::writeDTDAttlist — Écrit une liste d'attributs DTD
- XMLWriter::writeDTDElement — Écrit un élément DTD
- XMLWriter::writeDTDEntity — Écrit une entité DTD
- XMLWriter::writeDTD — Écrit une DTD
- XMLWriter::writeElementNS — Écrit un élément d'un espace de noms
- XMLWriter::writeElement — Écrit un élément
- XMLWriter::writePI — Écrit la balise PI
- XMLWriter::writeRaw — Écrit un texte XML brut
XMLWriter
25-Jul-2007 04:10
20-Nov-2006 10:23
Hi,
There already is a class xmlWriter, although it is not documented. This is a rewrite of the example from [http://cz2.php.net/manual/en/ref.xmlwriter.php#64484 Carlos Averett] and works well on my php 5.1.6
<?php
$xw = new xmlWriter();
$xw->openMemory();
$xw->startDocument('1.0','UTF-8');
$xw->startDtd('html','-//WAPFORUM//DTD XHTML Mobile 1.0//EN', 'http://www.wapforum.org/DTD/xhtml-mobile10.dtd');
$xw->endDtd();
$xw->startElement ('html'); // <html>
$xw->writeAttribute( 'xmlns', 'http://www.wapforum.org/DTD/xhtml-mobile10.dtd');
$xw->writeAttribute( 'xm:lang', 'en');
$xw->startElement('head'); // <head>
$xw->writeElement ('title', 'Test WAP Document');
$xw->endElement(); // </head>
$xw->startElement('body'); // <body>
$xw->startElement('ol'); // <ol>
$xw->writeElement ('li', 'One Item & <sss <ss />></ss>');
$xw->writeElement ('li', 'Another Item');
$xw->writeElement ('li', 'Another Item');
$xw->endElement(); // </ol>
$xw->endElement(); // </body>
$xw->endElement(); // </html>
$xw->endDtd();
print $xw->outputMemory(true);
?>
16-Apr-2006 02:25
How to generate a simple XML document for a XSL-Transformation purpose.
We have 3 files:
- 'index.php' : output a XML document
- 'XmlConstruct.class.php' : allow to construct a XML document with 'xmlwriter'
- 'index.xsl' : contains a XSLT document
Contents of the file 'index.php' :
<?php
$contents = array('page_title' => 'Generate a XHTML page from XML+XSLT files',
'welcome_msg' => 'Simple XHTML document from XML+XSLT files!');
require('XmlConstruct.class.php');
$XmlConstruct = new XmlConstruct('rootElement', 'index.xsl');
$XmlConstruct->fromArray($contents);
$XmlConstruct->output();
?>
Contents of the file 'XmlConstruct.class.php' :
<?php
/**
* Construct a simple XML document.
* This class inherits from the (PHP) class 'xmlwriter'.
* You will need at least PHP 5.1.2
*
* @author Alexandre Arica
* @since 16 April 2006
* @version 1.0 modified the 16 April 2006
*/
class XmlConstruct extends XMLWriter
{
/**
* Constructor.
* @param string $prm_rootElementName A root element's name of a current xml document
* @param string $prm_xsltFilePath Path of a XSLT file.
* @access public
* @param null
*/
public function __construct($prm_rootElementName, $prm_xsltFilePath=''){
$this->openMemory();
$this->setIndent(true);
$this->setIndentString(' ');
$this->startDocument('1.0', 'UTF-8');
if($prm_xsltFilePath){
$this->writePi('xml-stylesheet', 'type="text/xsl" href="'.$prm_xsltFilePath.'"');
}
$this->startElement($prm_rootElementName);
}
/**
* Set an element with a text to a current xml document.
* @access public
* @param string $prm_elementName An element's name
* @param string $prm_ElementText An element's text
* @return null
*/
public function setElement($prm_elementName, $prm_ElementText){
$this->startElement($prm_elementName);
$this->text($prm_ElementText);
$this->endElement();
}
/**
* Construct elements and texts from an array.
* The array should contain an attribute's name in index part
* and a attribute's text in value part.
* @access public
* @param array $prm_array Contains attributes and texts
* @return null
*/
public function fromArray($prm_array){
if(is_array($prm_array)){
foreach ($prm_array as $index => $text){
$this->setElement($index, $text);
}
}
}
/**
* Return the content of a current xml document.
* @access public
* @param null
* @return string Xml document
*/
public function getDocument(){
$this->endElement();
$this->endDocument();
return $this->outputMemory();
}
/**
* Output the content of a current xml document.
* @access public
* @param null
*/
public function output(){
header('Content-type: text/xml');
echo $this->getDocument();
}
}
?>
Contents of the file 'index.xsl' :
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/TR/xhtml1/strict">
<xsl:output
method="html"
encoding="utf-8"
/>
<xsl:template match="rootElement">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><xsl:value-of select="page_title" /></title>
</head>
<body>
<xsl:value-of select="welcome_msg" />
</body>
</html>
</xsl:template>
</xsl:stylesheet>
15-Apr-2006 07:49
Using XMLWriter to create a WAP page:
<?php
$memory = xmlwriter_open_memory();
xmlwriter_start_document($memory,'1.0','UTF-8');
xmlwriter_start_dtd($memory,'html','-//WAPFORUM//DTD XHTML Mobile 1.0//EN', 'http://www.wapforum.org/DTD/xhtml-mobile10.dtd');
xmlwriter_end_dtd($memory);
xmlwriter_start_element ($memory,'html'); // <html>
xmlwriter_write_attribute( $memory, 'xmlns', 'http://www.wapforum.org/DTD/xhtml-mobile10.dtd');
xmlwriter_write_attribute( $memory, 'xm:lang', 'en');
xmlwriter_start_element($memory,'head'); // <head>
xmlwriter_write_element ($memory,'title', 'Test WAP Document');
xmlwriter_end_element($memory); // </head>
xmlwriter_start_element($memory,'body'); // <body>
xmlwriter_start_element($memory,'ol'); // <ol>
xmlwriter_write_element ($memory,'li', 'One Item');
xmlwriter_write_element ($memory,'li', 'Another Item');
xmlwriter_write_element ($memory,'li', 'Another Item');
xmlwriter_end_element($memory); // </ol>
xmlwriter_end_element($memory); // </body>
xmlwriter_end_element($memory); // </html>
xmlwriter_end_dtd($memory);
$xml = xmlwriter_output_memory($memory,true);
?>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.wapforum.org/DTD/xhtml-mobile10.dtd" xm:lang="en">
<head>
<title>Test WAP Document</title>
</head>
<body>
<ol>
<li>One Item</li>
<li>Another Item</li>
<li>Another Item</li>
</ol>
</body>
</html>
