Edit

kc3-lang/libxml2/doc/examples/testWriter.c

Branch :

  • Show log

    Commit

  • Author : Daniel Veillard
    Date : 2004-10-31 15:35:32
    Hash : 05f9735b
    Message : Fixed bug #153937, making sure the conversion functions return the number * encoding.c doc/examples/testWriter.c: Fixed bug #153937, making sure the conversion functions return the number of byte written. Had to fix one of the examples. Daniel

  • doc/examples/testWriter.c
  • /**
     * 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: <