1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198 |
- /**
- * 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 writer.xml writer$$i.res ; done ; rm writer*.res
- * 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.res");
- /* next, the memory version */
- testXmlwriterMemory("writer2.res");
- /* next, the DOM version */
- testXmlwriterDoc("writer3.res");
- /* next, the tree version */
- testXmlwriterTree("writer4.res");
- /*
- * 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: <äöü>",
- 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("<äöü>", 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", 53535L);
- 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ü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ö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: <äöü>",
- 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("<äöü>", 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", 53535L);
- 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ü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ö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: <äöü>",
- 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("<äöü>", 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", 53535L);
- 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ü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ö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: <äöü>",
- 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("<äöü>", 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", 53535L);
- 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ü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ö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
|