1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-03-19 14:50:07 +03:00
libxml2/doc/examples/testWriter.c
Daniel Richard G 6842ee810e More cleanups to the documentation part of libxml2
doc/Makefile.am:

* Build what's in doc/ before doc/devhelp/, as the dependency graph flows
   that way

* Add "--path $(srcdir)" so that xsltproc can find DTDs in srcdir

* Replaced $(top_srcdir)/doc with an equivalent $(srcdir)

* Qualified libxml2-api.xml with $(srcdir) as it's always generated there

* Rewrote the dependencies for libxml2-api.xml so that xmlversion.h
   doesn't throw everything off

doc/devhelp/Makefile.am:

* Use Automake constructs to install the HTML files instead of an
   install-data-local rule

* Reorganized the file a bit (hello whitespace!)

* EXTRA_DIST doesn't need to list so many files now that dist_devhelp_DATA
   is being used

* Only print "Rebuilding devhelp files" if rebuilding is actually
   occurring

doc/examples/index.py:

* Make the "this file is auto-generated" banner more prominent

* Autotools updates: Use AM_CPPFLAGS/AM_CFLAGS instead of INCLUDES

* Got rid of DEPS as it's not needed (Automake already sees the dependency
   on libxml2.la by way of LDADD(S))

* Replaced LDADDS with LDADD, which is applied to all programs listed
   in the file. Since all the test programs have the same link
   dependencies, this way is more concise yet equivalent.

* Remove the *.tmp files via "make clean" instead of having the test
   programs do it themselves (more on this later)

* Invoke index.py in srcdir, as it pretty much needs to run there

* Restructured the index.html rule so that only the xmllint invocation is
   allowed to fail

* Use $(MKDIR_P) instead of $(mkinstalldirs), $(VAR) instead of @VAR@

* Remove symlinks for test?.xml in an out-of-source build

* Sort lists for neatness

* Better formatting for EXTRA_DIST and noinst_PROGRAMS variables

* Simplified the Automake bits printed for each program: *_LDFLAGS doesn't
   need to be specified as it's empty anyway, *_DEPENDENCIES is redundant,
   *_LDADD isn't needed due to the global LDADD

* Added a bit that symlinks in test?.xml from srcdir in out-of-source
   builds. This allows the reader4 test to read these files in the current
   directory, which ensures that the output always looks the same (i.e.
   does not contain references to srcdir)

* Don't hide the test program invocation (or else it's hard to tell which
   test failed), and don't use superfluous parentheses

* NOTE: If you check in these changes, be sure to run this script and also
   check in the updated files that it generates!

doc/examples/*.c:

* Updated the test: lines so that

   + "&&" is used to separate commands instead of ";" so that errors are
     not masked

   + reference files are qualified with $(srcdir)/

   + no "rm" takes place -- these are a problem because (1) if a test
     fails, it's useful to have the output file ready for inspection; (2)
     the "rm" invocation masks a potential non-zero exit status from diff

     (This is why I added the CLEANFILES line above)

doc/examples/io1.res:

* Updated this ref file so that the test passes. (This is correct, right?)

doc/examples/reader4.res:

* Changed this back to its original form, as the symlinking of test?.xml
   means this file no longer has to contain path prefixes on the filenames

doc/examples/testWriter.c:

* Changed the output filenames to *.tmp instead of *.res, partly for
   consistency, partly to not have to add special cases to CLEANFILES

doc/examples/xpath1.c:

* Removed the "./" prefix on the test invocation, which is redundant as
   index.py already adds one
2012-08-17 09:58:38 +08:00

1199 lines
38 KiB
C
Raw Permalink Blame History

/**
* section: xmlWriter
* synopsis: use various APIs for the xmlWriter
* purpose: tests a number of APIs for the xmlWriter, especially
* the various methods to write to a filename, to a memory
* buffer, to a new document, or to a subtree. It shows how to
* do encoding string conversions too. The resulting
* documents are then serialized.
* usage: testWriter
* test: testWriter && for i in 1 2 3 4 ; do diff $(srcdir)/writer.xml writer$$i.tmp || break ; done
* author: Alfred Mickautsch
* copy: see Copyright for the status of this software.
*/
#include <stdio.h>
#include <string.h>
#include <libxml/encoding.h>
#include <libxml/xmlwriter.h>
#if defined(LIBXML_WRITER_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
#define MY_ENCODING "ISO-8859-1"
void testXmlwriterFilename(const char *uri);
void testXmlwriterMemory(const char *file);
void testXmlwriterDoc(const char *file);
void testXmlwriterTree(const char *file);
xmlChar *ConvertInput(const char *in, const char *encoding);
int
main(void)
{
/*
* this initialize the library and check potential ABI mismatches
* between the version it was compiled for and the actual shared
* library used.
*/
LIBXML_TEST_VERSION
/* first, the file version */
testXmlwriterFilename("writer1.tmp");
/* next, the memory version */
testXmlwriterMemory("writer2.tmp");
/* next, the DOM version */
testXmlwriterDoc("writer3.tmp");
/* next, the tree version */
testXmlwriterTree("writer4.tmp");
/*
* Cleanup function for the XML library.
*/
xmlCleanupParser();
/*
* this is to debug memory for regression tests
*/
xmlMemoryDump();
return 0;
}
/**
* testXmlwriterFilename:
* @uri: the output URI
*
* test the xmlWriter interface when writing to a new file
*/
void
testXmlwriterFilename(const char *uri)
{
int rc;
xmlTextWriterPtr writer;
xmlChar *tmp;
/* Create a new XmlWriter for uri, with no compression. */
writer = xmlNewTextWriterFilename(uri, 0);
if (writer == NULL) {
printf("testXmlwriterFilename: Error creating the xml writer\n");
return;
}
/* Start the document with the xml default for the version,
* encoding ISO 8859-1 and the default for the standalone
* declaration. */
rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterStartDocument\n");
return;
}
/* Start an element named "EXAMPLE". Since thist is the first
* element, this will be the root element of the document. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "EXAMPLE");
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
return;
}
/* Write a comment as child of EXAMPLE.
* Please observe, that the input to the xmlTextWriter functions
* HAS to be in UTF-8, even if the output XML is encoded
* in iso-8859-1 */
tmp = ConvertInput("This is a comment with special chars: <<3C><><EFBFBD>>",
MY_ENCODING);
rc = xmlTextWriterWriteComment(writer, tmp);
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterWriteComment\n");
return;
}
if (tmp != NULL) xmlFree(tmp);
/* Start an element named "ORDER" as child of EXAMPLE. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "ORDER");
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
return;
}
/* Add an attribute with name "version" and value "1.0" to ORDER. */
rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "version",
BAD_CAST "1.0");
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterWriteAttribute\n");
return;
}
/* Add an attribute with name "xml:lang" and value "de" to ORDER. */
rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
BAD_CAST "de");
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterWriteAttribute\n");
return;
}
/* Write a comment as child of ORDER */
tmp = ConvertInput("<<3C><><EFBFBD>>", MY_ENCODING);
rc = xmlTextWriterWriteFormatComment(writer,
"This is another comment with special chars: %s",
tmp);
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterWriteFormatComment\n");
return;
}
if (tmp != NULL) xmlFree(tmp);
/* Start an element named "HEADER" as child of ORDER. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "HEADER");
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
return;
}
/* Write an element named "X_ORDER_ID" as child of HEADER. */
rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "X_ORDER_ID",
"%010d", 53535);
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
return;
}
/* Write an element named "CUSTOMER_ID" as child of HEADER. */
rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "CUSTOMER_ID",
"%d", 1010);
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
return;
}
/* Write an element named "NAME_1" as child of HEADER. */
tmp = ConvertInput("M<EFBFBD>ller", MY_ENCODING);
rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_1", tmp);
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterWriteElement\n");
return;
}
if (tmp != NULL) xmlFree(tmp);
/* Write an element named "NAME_2" as child of HEADER. */
tmp = ConvertInput("J<EFBFBD>rg", MY_ENCODING);
rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_2", tmp);
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterWriteElement\n");
return;
}
if (tmp != NULL) xmlFree(tmp);
/* Close the element named HEADER. */
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
return;
}
/* Start an element named "ENTRIES" as child of ORDER. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRIES");
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
return;
}
/* Start an element named "ENTRY" as child of ENTRIES. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
return;
}
/* Write an element named "ARTICLE" as child of ENTRY. */
rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
BAD_CAST "<Test>");
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterWriteElement\n");
return;
}
/* Write an element named "ENTRY_NO" as child of ENTRY. */
rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
10);
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
return;
}
/* Close the element named ENTRY. */
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
return;
}
/* Start an element named "ENTRY" as child of ENTRIES. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
return;
}
/* Write an element named "ARTICLE" as child of ENTRY. */
rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
BAD_CAST "<Test 2>");
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterWriteElement\n");
return;
}
/* Write an element named "ENTRY_NO" as child of ENTRY. */
rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
20);
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterWriteFormatElement\n");
return;
}
/* Close the element named ENTRY. */
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
return;
}
/* Close the element named ENTRIES. */
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
return;
}
/* Start an element named "FOOTER" as child of ORDER. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "FOOTER");
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterStartElement\n");
return;
}
/* Write an element named "TEXT" as child of FOOTER. */
rc = xmlTextWriterWriteElement(writer, BAD_CAST "TEXT",
BAD_CAST "This is a text.");
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterWriteElement\n");
return;
}
/* Close the element named FOOTER. */
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterEndElement\n");
return;
}
/* Here we could close the elements ORDER and EXAMPLE using the
* function xmlTextWriterEndElement, but since we do not want to
* write any other elements, we simply call xmlTextWriterEndDocument,
* which will do all the work. */
rc = xmlTextWriterEndDocument(writer);
if (rc < 0) {
printf
("testXmlwriterFilename: Error at xmlTextWriterEndDocument\n");
return;
}
xmlFreeTextWriter(writer);
}
/**
* testXmlwriterMemory:
* @file: the output file
*
* test the xmlWriter interface when writing to memory
*/
void
testXmlwriterMemory(const char *file)
{
int rc;
xmlTextWriterPtr writer;
xmlBufferPtr buf;
xmlChar *tmp;
FILE *fp;
/* Create a new XML buffer, to which the XML document will be
* written */
buf = xmlBufferCreate();
if (buf == NULL) {
printf("testXmlwriterMemory: Error creating the xml buffer\n");
return;
}
/* Create a new XmlWriter for memory, with no compression.
* Remark: there is no compression for this kind of xmlTextWriter */
writer = xmlNewTextWriterMemory(buf, 0);
if (writer == NULL) {
printf("testXmlwriterMemory: Error creating the xml writer\n");
return;
}
/* Start the document with the xml default for the version,
* encoding ISO 8859-1 and the default for the standalone
* declaration. */
rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
if (rc < 0) {
printf
("testXmlwriterMemory: Error at xmlTextWriterStartDocument\n");
return;
}
/* Start an element named "EXAMPLE". Since thist is the first
* element, this will be the root element of the document. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "EXAMPLE");
if (rc < 0) {
printf
("testXmlwriterMemory: Error at xmlTextWriterStartElement\n");
return;
}
/* Write a comment as child of EXAMPLE.
* Please observe, that the input to the xmlTextWriter functions
* HAS to be in UTF-8, even if the output XML is encoded
* in iso-8859-1 */
tmp = ConvertInput("This is a comment with special chars: <<3C><><EFBFBD>>",
MY_ENCODING);
rc = xmlTextWriterWriteComment(writer, tmp);
if (rc < 0) {
printf
("testXmlwriterMemory: Error at xmlTextWriterWriteComment\n");
return;
}
if (tmp != NULL) xmlFree(tmp);
/* Start an element named "ORDER" as child of EXAMPLE. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "ORDER");
if (rc < 0) {
printf
("testXmlwriterMemory: Error at xmlTextWriterStartElement\n");
return;
}
/* Add an attribute with name "version" and value "1.0" to ORDER. */
rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "version",
BAD_CAST "1.0");
if (rc < 0) {
printf
("testXmlwriterMemory: Error at xmlTextWriterWriteAttribute\n");
return;
}
/* Add an attribute with name "xml:lang" and value "de" to ORDER. */
rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
BAD_CAST "de");
if (rc < 0) {
printf
("testXmlwriterMemory: Error at xmlTextWriterWriteAttribute\n");
return;
}
/* Write a comment as child of ORDER */
tmp = ConvertInput("<<3C><><EFBFBD>>", MY_ENCODING);
rc = xmlTextWriterWriteFormatComment(writer,
"This is another comment with special chars: %s",
tmp);
if (rc < 0) {
printf
("testXmlwriterMemory: Error at xmlTextWriterWriteFormatComment\n");
return;
}
if (tmp != NULL) xmlFree(tmp);
/* Start an element named "HEADER" as child of ORDER. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "HEADER");
if (rc < 0) {
printf
("testXmlwriterMemory: Error at xmlTextWriterStartElement\n");
return;
}
/* Write an element named "X_ORDER_ID" as child of HEADER. */
rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "X_ORDER_ID",
"%010d", 53535);
if (rc < 0) {
printf
("testXmlwriterMemory: Error at xmlTextWriterWriteFormatElement\n");
return;
}
/* Write an element named "CUSTOMER_ID" as child of HEADER. */
rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "CUSTOMER_ID",
"%d", 1010);
if (rc < 0) {
printf
("testXmlwriterMemory: Error at xmlTextWriterWriteFormatElement\n");
return;
}
/* Write an element named "NAME_1" as child of HEADER. */
tmp = ConvertInput("M<EFBFBD>ller", MY_ENCODING);
rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_1", tmp);
if (rc < 0) {
printf
("testXmlwriterMemory: Error at xmlTextWriterWriteElement\n");
return;
}
if (tmp != NULL) xmlFree(tmp);
/* Write an element named "NAME_2" as child of HEADER. */
tmp = ConvertInput("J<EFBFBD>rg", MY_ENCODING);
rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_2", tmp);
if (rc < 0) {
printf
("testXmlwriterMemory: Error at xmlTextWriterWriteElement\n");
return;
}
if (tmp != NULL) xmlFree(tmp);
/* Close the element named HEADER. */
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
printf("testXmlwriterMemory: Error at xmlTextWriterEndElement\n");
return;
}
/* Start an element named "ENTRIES" as child of ORDER. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRIES");
if (rc < 0) {
printf
("testXmlwriterMemory: Error at xmlTextWriterStartElement\n");
return;
}
/* Start an element named "ENTRY" as child of ENTRIES. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
if (rc < 0) {
printf
("testXmlwriterMemory: Error at xmlTextWriterStartElement\n");
return;
}
/* Write an element named "ARTICLE" as child of ENTRY. */
rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
BAD_CAST "<Test>");
if (rc < 0) {
printf
("testXmlwriterMemory: Error at xmlTextWriterWriteElement\n");
return;
}
/* Write an element named "ENTRY_NO" as child of ENTRY. */
rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
10);
if (rc < 0) {
printf
("testXmlwriterMemory: Error at xmlTextWriterWriteFormatElement\n");
return;
}
/* Close the element named ENTRY. */
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
printf("testXmlwriterMemory: Error at xmlTextWriterEndElement\n");
return;
}
/* Start an element named "ENTRY" as child of ENTRIES. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
if (rc < 0) {
printf
("testXmlwriterMemory: Error at xmlTextWriterStartElement\n");
return;
}
/* Write an element named "ARTICLE" as child of ENTRY. */
rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
BAD_CAST "<Test 2>");
if (rc < 0) {
printf
("testXmlwriterMemory: Error at xmlTextWriterWriteElement\n");
return;
}
/* Write an element named "ENTRY_NO" as child of ENTRY. */
rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
20);
if (rc < 0) {
printf
("testXmlwriterMemory: Error at xmlTextWriterWriteFormatElement\n");
return;
}
/* Close the element named ENTRY. */
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
printf("testXmlwriterMemory: Error at xmlTextWriterEndElement\n");
return;
}
/* Close the element named ENTRIES. */
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
printf("testXmlwriterMemory: Error at xmlTextWriterEndElement\n");
return;
}
/* Start an element named "FOOTER" as child of ORDER. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "FOOTER");
if (rc < 0) {
printf
("testXmlwriterMemory: Error at xmlTextWriterStartElement\n");
return;
}
/* Write an element named "TEXT" as child of FOOTER. */
rc = xmlTextWriterWriteElement(writer, BAD_CAST "TEXT",
BAD_CAST "This is a text.");
if (rc < 0) {
printf
("testXmlwriterMemory: Error at xmlTextWriterWriteElement\n");
return;
}
/* Close the element named FOOTER. */
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
printf("testXmlwriterMemory: Error at xmlTextWriterEndElement\n");
return;
}
/* Here we could close the elements ORDER and EXAMPLE using the
* function xmlTextWriterEndElement, but since we do not want to
* write any other elements, we simply call xmlTextWriterEndDocument,
* which will do all the work. */
rc = xmlTextWriterEndDocument(writer);
if (rc < 0) {
printf("testXmlwriterMemory: Error at xmlTextWriterEndDocument\n");
return;
}
xmlFreeTextWriter(writer);
fp = fopen(file, "w");
if (fp == NULL) {
printf("testXmlwriterMemory: Error at fopen\n");
return;
}
fprintf(fp, "%s", (const char *) buf->content);
fclose(fp);
xmlBufferFree(buf);
}
/**
* testXmlwriterDoc:
* @file: the output file
*
* test the xmlWriter interface when creating a new document
*/
void
testXmlwriterDoc(const char *file)
{
int rc;
xmlTextWriterPtr writer;
xmlChar *tmp;
xmlDocPtr doc;
/* Create a new XmlWriter for DOM, with no compression. */
writer = xmlNewTextWriterDoc(&doc, 0);
if (writer == NULL) {
printf("testXmlwriterDoc: Error creating the xml writer\n");
return;
}
/* Start the document with the xml default for the version,
* encoding ISO 8859-1 and the default for the standalone
* declaration. */
rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterStartDocument\n");
return;
}
/* Start an element named "EXAMPLE". Since thist is the first
* element, this will be the root element of the document. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "EXAMPLE");
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterStartElement\n");
return;
}
/* Write a comment as child of EXAMPLE.
* Please observe, that the input to the xmlTextWriter functions
* HAS to be in UTF-8, even if the output XML is encoded
* in iso-8859-1 */
tmp = ConvertInput("This is a comment with special chars: <<3C><><EFBFBD>>",
MY_ENCODING);
rc = xmlTextWriterWriteComment(writer, tmp);
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterWriteComment\n");
return;
}
if (tmp != NULL) xmlFree(tmp);
/* Start an element named "ORDER" as child of EXAMPLE. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "ORDER");
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterStartElement\n");
return;
}
/* Add an attribute with name "version" and value "1.0" to ORDER. */
rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "version",
BAD_CAST "1.0");
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterWriteAttribute\n");
return;
}
/* Add an attribute with name "xml:lang" and value "de" to ORDER. */
rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
BAD_CAST "de");
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterWriteAttribute\n");
return;
}
/* Write a comment as child of ORDER */
tmp = ConvertInput("<<3C><><EFBFBD>>", MY_ENCODING);
rc = xmlTextWriterWriteFormatComment(writer,
"This is another comment with special chars: %s",
tmp);
if (rc < 0) {
printf
("testXmlwriterDoc: Error at xmlTextWriterWriteFormatComment\n");
return;
}
if (tmp != NULL) xmlFree(tmp);
/* Start an element named "HEADER" as child of ORDER. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "HEADER");
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterStartElement\n");
return;
}
/* Write an element named "X_ORDER_ID" as child of HEADER. */
rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "X_ORDER_ID",
"%010d", 53535);
if (rc < 0) {
printf
("testXmlwriterDoc: Error at xmlTextWriterWriteFormatElement\n");
return;
}
/* Write an element named "CUSTOMER_ID" as child of HEADER. */
rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "CUSTOMER_ID",
"%d", 1010);
if (rc < 0) {
printf
("testXmlwriterDoc: Error at xmlTextWriterWriteFormatElement\n");
return;
}
/* Write an element named "NAME_1" as child of HEADER. */
tmp = ConvertInput("M<EFBFBD>ller", MY_ENCODING);
rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_1", tmp);
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterWriteElement\n");
return;
}
if (tmp != NULL) xmlFree(tmp);
/* Write an element named "NAME_2" as child of HEADER. */
tmp = ConvertInput("J<EFBFBD>rg", MY_ENCODING);
rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_2", tmp);
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterWriteElement\n");
return;
}
if (tmp != NULL) xmlFree(tmp);
/* Close the element named HEADER. */
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterEndElement\n");
return;
}
/* Start an element named "ENTRIES" as child of ORDER. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRIES");
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterStartElement\n");
return;
}
/* Start an element named "ENTRY" as child of ENTRIES. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterStartElement\n");
return;
}
/* Write an element named "ARTICLE" as child of ENTRY. */
rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
BAD_CAST "<Test>");
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterWriteElement\n");
return;
}
/* Write an element named "ENTRY_NO" as child of ENTRY. */
rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
10);
if (rc < 0) {
printf
("testXmlwriterDoc: Error at xmlTextWriterWriteFormatElement\n");
return;
}
/* Close the element named ENTRY. */
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterEndElement\n");
return;
}
/* Start an element named "ENTRY" as child of ENTRIES. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterStartElement\n");
return;
}
/* Write an element named "ARTICLE" as child of ENTRY. */
rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
BAD_CAST "<Test 2>");
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterWriteElement\n");
return;
}
/* Write an element named "ENTRY_NO" as child of ENTRY. */
rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
20);
if (rc < 0) {
printf
("testXmlwriterDoc: Error at xmlTextWriterWriteFormatElement\n");
return;
}
/* Close the element named ENTRY. */
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterEndElement\n");
return;
}
/* Close the element named ENTRIES. */
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterEndElement\n");
return;
}
/* Start an element named "FOOTER" as child of ORDER. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "FOOTER");
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterStartElement\n");
return;
}
/* Write an element named "TEXT" as child of FOOTER. */
rc = xmlTextWriterWriteElement(writer, BAD_CAST "TEXT",
BAD_CAST "This is a text.");
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterWriteElement\n");
return;
}
/* Close the element named FOOTER. */
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterEndElement\n");
return;
}
/* Here we could close the elements ORDER and EXAMPLE using the
* function xmlTextWriterEndElement, but since we do not want to
* write any other elements, we simply call xmlTextWriterEndDocument,
* which will do all the work. */
rc = xmlTextWriterEndDocument(writer);
if (rc < 0) {
printf("testXmlwriterDoc: Error at xmlTextWriterEndDocument\n");
return;
}
xmlFreeTextWriter(writer);
xmlSaveFileEnc(file, doc, MY_ENCODING);
xmlFreeDoc(doc);
}
/**
* testXmlwriterTree:
* @file: the output file
*
* test the xmlWriter interface when writing to a subtree
*/
void
testXmlwriterTree(const char *file)
{
int rc;
xmlTextWriterPtr writer;
xmlDocPtr doc;
xmlNodePtr node;
xmlChar *tmp;
/* Create a new XML DOM tree, to which the XML document will be
* written */
doc = xmlNewDoc(BAD_CAST XML_DEFAULT_VERSION);
if (doc == NULL) {
printf
("testXmlwriterTree: Error creating the xml document tree\n");
return;
}
/* Create a new XML node, to which the XML document will be
* appended */
node = xmlNewDocNode(doc, NULL, BAD_CAST "EXAMPLE", NULL);
if (node == NULL) {
printf("testXmlwriterTree: Error creating the xml node\n");
return;
}
/* Make ELEMENT the root node of the tree */
xmlDocSetRootElement(doc, node);
/* Create a new XmlWriter for DOM tree, with no compression. */
writer = xmlNewTextWriterTree(doc, node, 0);
if (writer == NULL) {
printf("testXmlwriterTree: Error creating the xml writer\n");
return;
}
/* Start the document with the xml default for the version,
* encoding ISO 8859-1 and the default for the standalone
* declaration. */
rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
if (rc < 0) {
printf("testXmlwriterTree: Error at xmlTextWriterStartDocument\n");
return;
}
/* Write a comment as child of EXAMPLE.
* Please observe, that the input to the xmlTextWriter functions
* HAS to be in UTF-8, even if the output XML is encoded
* in iso-8859-1 */
tmp = ConvertInput("This is a comment with special chars: <<3C><><EFBFBD>>",
MY_ENCODING);
rc = xmlTextWriterWriteComment(writer, tmp);
if (rc < 0) {
printf("testXmlwriterTree: Error at xmlTextWriterWriteComment\n");
return;
}
if (tmp != NULL) xmlFree(tmp);
/* Start an element named "ORDER" as child of EXAMPLE. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "ORDER");
if (rc < 0) {
printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
return;
}
/* Add an attribute with name "version" and value "1.0" to ORDER. */
rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "version",
BAD_CAST "1.0");
if (rc < 0) {
printf
("testXmlwriterTree: Error at xmlTextWriterWriteAttribute\n");
return;
}
/* Add an attribute with name "xml:lang" and value "de" to ORDER. */
rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xml:lang",
BAD_CAST "de");
if (rc < 0) {
printf
("testXmlwriterTree: Error at xmlTextWriterWriteAttribute\n");
return;
}
/* Write a comment as child of ORDER */
tmp = ConvertInput("<<3C><><EFBFBD>>", MY_ENCODING);
rc = xmlTextWriterWriteFormatComment(writer,
"This is another comment with special chars: %s",
tmp);
if (rc < 0) {
printf
("testXmlwriterTree: Error at xmlTextWriterWriteFormatComment\n");
return;
}
if (tmp != NULL) xmlFree(tmp);
/* Start an element named "HEADER" as child of ORDER. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "HEADER");
if (rc < 0) {
printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
return;
}
/* Write an element named "X_ORDER_ID" as child of HEADER. */
rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "X_ORDER_ID",
"%010d", 53535);
if (rc < 0) {
printf
("testXmlwriterTree: Error at xmlTextWriterWriteFormatElement\n");
return;
}
/* Write an element named "CUSTOMER_ID" as child of HEADER. */
rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "CUSTOMER_ID",
"%d", 1010);
if (rc < 0) {
printf
("testXmlwriterTree: Error at xmlTextWriterWriteFormatElement\n");
return;
}
/* Write an element named "NAME_1" as child of HEADER. */
tmp = ConvertInput("M<EFBFBD>ller", MY_ENCODING);
rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_1", tmp);
if (rc < 0) {
printf("testXmlwriterTree: Error at xmlTextWriterWriteElement\n");
return;
}
if (tmp != NULL) xmlFree(tmp);
/* Write an element named "NAME_2" as child of HEADER. */
tmp = ConvertInput("J<EFBFBD>rg", MY_ENCODING);
rc = xmlTextWriterWriteElement(writer, BAD_CAST "NAME_2", tmp);
if (rc < 0) {
printf("testXmlwriterTree: Error at xmlTextWriterWriteElement\n");
return;
}
if (tmp != NULL) xmlFree(tmp);
/* Close the element named HEADER. */
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
printf("testXmlwriterTree: Error at xmlTextWriterEndElement\n");
return;
}
/* Start an element named "ENTRIES" as child of ORDER. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRIES");
if (rc < 0) {
printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
return;
}
/* Start an element named "ENTRY" as child of ENTRIES. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
if (rc < 0) {
printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
return;
}
/* Write an element named "ARTICLE" as child of ENTRY. */
rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
BAD_CAST "<Test>");
if (rc < 0) {
printf("testXmlwriterTree: Error at xmlTextWriterWriteElement\n");
return;
}
/* Write an element named "ENTRY_NO" as child of ENTRY. */
rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
10);
if (rc < 0) {
printf
("testXmlwriterTree: Error at xmlTextWriterWriteFormatElement\n");
return;
}
/* Close the element named ENTRY. */
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
printf("testXmlwriterTree: Error at xmlTextWriterEndElement\n");
return;
}
/* Start an element named "ENTRY" as child of ENTRIES. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "ENTRY");
if (rc < 0) {
printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
return;
}
/* Write an element named "ARTICLE" as child of ENTRY. */
rc = xmlTextWriterWriteElement(writer, BAD_CAST "ARTICLE",
BAD_CAST "<Test 2>");
if (rc < 0) {
printf("testXmlwriterTree: Error at xmlTextWriterWriteElement\n");
return;
}
/* Write an element named "ENTRY_NO" as child of ENTRY. */
rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENTRY_NO", "%d",
20);
if (rc < 0) {
printf
("testXmlwriterTree: Error at xmlTextWriterWriteFormatElement\n");
return;
}
/* Close the element named ENTRY. */
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
printf("testXmlwriterTree: Error at xmlTextWriterEndElement\n");
return;
}
/* Close the element named ENTRIES. */
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
printf("testXmlwriterTree: Error at xmlTextWriterEndElement\n");
return;
}
/* Start an element named "FOOTER" as child of ORDER. */
rc = xmlTextWriterStartElement(writer, BAD_CAST "FOOTER");
if (rc < 0) {
printf("testXmlwriterTree: Error at xmlTextWriterStartElement\n");
return;
}
/* Write an element named "TEXT" as child of FOOTER. */
rc = xmlTextWriterWriteElement(writer, BAD_CAST "TEXT",
BAD_CAST "This is a text.");
if (rc < 0) {
printf("testXmlwriterTree: Error at xmlTextWriterWriteElement\n");
return;
}
/* Close the element named FOOTER. */
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
printf("testXmlwriterTree: Error at xmlTextWriterEndElement\n");
return;
}
/* Here we could close the elements ORDER and EXAMPLE using the
* function xmlTextWriterEndElement, but since we do not want to
* write any other elements, we simply call xmlTextWriterEndDocument,
* which will do all the work. */
rc = xmlTextWriterEndDocument(writer);
if (rc < 0) {
printf("testXmlwriterTree: Error at xmlTextWriterEndDocument\n");
return;
}
xmlFreeTextWriter(writer);
xmlSaveFileEnc(file, doc, MY_ENCODING);
xmlFreeDoc(doc);
}
/**
* ConvertInput:
* @in: string in a given encoding
* @encoding: the encoding used
*
* Converts @in into UTF-8 for processing with libxml2 APIs
*
* Returns the converted UTF-8 string, or NULL in case of error.
*/
xmlChar *
ConvertInput(const char *in, const char *encoding)
{
xmlChar *out;
int ret;
int size;
int out_size;
int temp;
xmlCharEncodingHandlerPtr handler;
if (in == 0)
return 0;
handler = xmlFindCharEncodingHandler(encoding);
if (!handler) {
printf("ConvertInput: no encoding handler found for '%s'\n",
encoding ? encoding : "");
return 0;
}
size = (int) strlen(in) + 1;
out_size = size * 2 - 1;
out = (unsigned char *) xmlMalloc((size_t) out_size);
if (out != 0) {
temp = size - 1;
ret = handler->input(out, &out_size, (const xmlChar *) in, &temp);
if ((ret < 0) || (temp - size + 1)) {
if (ret < 0) {
printf("ConvertInput: conversion wasn't successful.\n");
} else {
printf
("ConvertInput: conversion wasn't successful. converted: %i octets.\n",
temp);
}
xmlFree(out);
out = 0;
} else {
out = (unsigned char *) xmlRealloc(out, out_size + 1);
out[out_size] = 0; /*null terminating out */
}
} else {
printf("ConvertInput: no mem\n");
}
return out;
}
#else
int main(void) {
fprintf(stderr, "Writer or output support not compiled in\n");
exit(1);
}
#endif