Fix bug #75343: Missing code examples for XMLWriter

Basically, we use the examples given in php-src.

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@343246 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Christoph Michael Becker 2017-10-15 13:29:48 +00:00
parent 71dcffaddf
commit 80fb006a12
2 changed files with 179 additions and 0 deletions

View file

@ -24,6 +24,7 @@
&reference.xmlwriter.setup;
&reference.xmlwriter.constants;
&reference.xmlwriter.examples;
&reference.xmlwriter.reference;
</book>

View file

@ -0,0 +1,178 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<chapter xml:id="xmlwriter.examples" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
&reftitle.examples;
<section xml:id="example.xmlwriter-simple">
<title>Creating a simple XML document</title>
<para>
This example shows how to use XMLWriter to create a XML document in memory.
</para>
<para>
<example>
<title>Creating a simple XML document</title>
<programlisting role="php">
<![CDATA[
<?php
$xw = xmlwriter_open_memory();
xmlwriter_set_indent($xw, 1);
$res = xmlwriter_set_indent_string($xw, ' ');
xmlwriter_start_document($xw, '1.0', 'UTF-8');
// A first element
xmlwriter_start_element($xw, 'tag1');
// Attribute 'att1' for element 'tag1'
xmlwriter_start_attribute($xw, 'att1');
xmlwriter_text($xw, 'valueofatt1');
xmlwriter_end_attribute($xw);
xmlwriter_write_comment($xw, 'this is a comment.');
// Start a child element
xmlwriter_start_element($xw, 'tag11');
xmlwriter_text($xw, 'This is a sample text, ä');
xmlwriter_end_element($xw); // tag11
xmlwriter_end_element($xw); // tag1
// CDATA
xmlwriter_start_element($xw, 'testc');
xmlwriter_write_cdata($xw, "This is cdata content");
xmlwriter_end_element($xw); // testc
xmlwriter_start_element($xw, 'testc');
xmlwriter_start_cdata($xw);
xmlwriter_text($xw, "test cdata2");
xmlwriter_end_cdata($xw);
xmlwriter_end_element($xw); // testc
// A processing instruction
xmlwriter_start_pi($xw, 'php');
xmlwriter_text($xw, '$foo=2;echo $foo;');
xmlwriter_end_pi($xw);
xmlwriter_end_document($xw);
echo xmlwriter_output_memory($xw);
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
<tag1 att1="valueofatt1">
<!--this is a comment.-->
<tag11>This is a sample text, ä</tag11>
</tag1>
<testc><![CDATA[This is cdata content]]]]><![CDATA[></testc>
<testc><![CDATA[test cdata2]]]]><![CDATA[></testc>
<?php $foo=2;echo $foo;?>
]]>
</screen>
</example>
</para>
</section>
<section xml:id="example.xmlwriter-namespace">
<title>Working with XML namespaces</title>
<para>
This example shows how to create namespaced XML elements.
</para>
<para>
<example>
<title>Working with XML namespaces</title>
<programlisting role="php">
<![CDATA[
<?php
$xw = xmlwriter_open_memory();
xmlwriter_set_indent($xw, 1);
$res = xmlwriter_set_indent_string($xw, ' ');
xmlwriter_start_document($xw, '1.0', 'UTF-8');
// A first element
xmlwriter_start_element_ns($xw,'prefix', 'books', 'uri');
xmlwriter_start_attribute($xw, 'isbn');
xmlwriter_start_attribute_ns($xw, 'prefix', 'isbn', 'uri');
xmlwriter_end_attribute($xw);
xmlwriter_end_attribute($xw);
xmlwriter_text($xw, 'book1');
xmlwriter_end_element($xw);
xmlwriter_end_document($xw);
echo xmlwriter_output_memory($xw);
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
<prefix:books isbn="" prefix:isbn="" xmlns:prefix="uri">book1</prefix:books>
]]>
</screen>
</example>
</para>
</section>
<section xml:id="example.xmlwriter-oop">
<title>Working with the OO API</title>
<para>
This example shows how to work with XMLWriter's object oriented API.
</para>
<para>
<example>
<title>Working with the OO API</title>
<programlisting role="php">
<![CDATA[
<?php
$xw = new XMLWriter();
$xw->openMemory();
$xw->startDocument("1.0");
$xw->startElement("book");
$xw->text("example");
$xw->endElement();
$xw->endDocument();
echo $xw->outputMemory();
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
<?xml version="1.0"?>
<book>example</book>
]]>
</screen>
</example>
</para>
</section>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->