[xml] FW: help - xmlwriter->to buffer & xmlreader from that buffer... whit libxml2



 

Dear all,

I need some help in writing to a buffer and reading from that buffer this is part of the code I use[2], in the end of this fist part this code writes do a document, in my application we can’t write files (cpu & time restriction), we intent to create a buffer to be sent to other clients. I’m rending from a file like the code bellow [2]

 

[1]________________________________________________________________________________________________________________________________

/**

 * refbox_xml_msg_creat:

 * @file: the output file

 *

 * test the xmlWriter interface when writing to memory

 */

 

void

refbox_xml_msg_creat(const char *file, refstatic *ref_msg_gen)

{

    int rc;

    xmlTextWriterPtr writer;

    xmlBufferPtr buf;

    FILE *fp;

 

    /* Create a new XML buffer, to which the XML document will be

     * written */

    buf = xmlBufferCreate();

    if (buf == NULL) {

        printf("refbox_xml_msg_creat: 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("refbox_xml_msg_creat: Error creating the xml writer\n");

        return;

    }

 

    /* Start the document with the xml default for the version,

     * encoding utf-8 and the default for the standalone

     * declaration. */

    rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);

    if (rc < 0) {

        printf

            ("refbox_xml_msg_creat: 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 "refcall");

    if (rc < 0) {

        printf

            ("refbox_xml_msg_creat: 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 */

    /*rc = xmlTextWriterWriteComment(writer,

                                   ConvertInput

                                   ("This is a comment with special chars: <äöü>",

                                    MY_ENCODING));*/

    /*if (rc < 0) {

        printf

            ("refbox_xml_msg_creat: Error at xmlTextWriterWriteComment\n");

        return;

    }*

 

/* Add an attribute with name "gameState" and value "on_off_int" to refcall. */

    rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "gameState",

                                     ref_msg_gen->state);

    if (rc < 0) {

        printf

            ("refbox_xml_msg_creat: Error at xmlTextWriterWriteAttribute\n");

        return;

    }

 

    /* Add an attribute with name "id" and value "#10" to refcall. */

    rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "id",ref_msg_gen->id);

    if (rc < 0) {

        printf

            ("refbox_xml_msg_creat: Error at xmlTextWriterWriteAttribute\n");

        return;

    }

 

 

 

    /* Start an element named "playerIn" as child of refcall. */

    rc = xmlTextWriterStartElement(writer, ref_msg_gen->command);

    if (rc < 0) {

        printf

            ("refbox_xml_msg_creat: Error at xmlTextWriterStartElement\n");

        return;

    }

 

    /* Add an attribute with name "requestID" and value "a-b" to playerIN. */

    rc = xmlTextWriterWriteAttribute(writer, ref_msg_gen->attrib,

                                     ref_msg_gen->val_attrib);

    if (rc < 0) {

        printf

            ("refbox_xml_msg_creat: Error at xmlTextWriterWriteAttribute\n");

        return;

    }

 

    /* Add an attribute with name "decicion" and value "accept|denied" to playerIN. */

    rc = xmlTextWriterWriteAttribute(writer, ref_msg_gen->attrib2,

                                    ref_msg_gen->val_attrib2);

    if (rc < 0) {

        printf

            ("refbox_xml_msg_creat: Error at xmlTextWriterWriteAttribute\n");

        return;

    }

 

 

 

    /* Add an attribute with name "decicion" and value "accept|denied" to playerIN. */

    rc = xmlTextWriterWriteAttribute(writer, ref_msg_gen->attrib3,

                                     ref_msg_gen->val_attrib3);

    if (rc < 0) {

        printf

            ("refbox_xml_msg_creat: Error at xmlTextWriterWriteAttribute\n");

        return;

    }

 

   /*Close the element named playerIn. */

  rc = xmlTextWriterEndElement(writer);

    if (rc < 0) {

        printf("refbox_xml_msg_creat: 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("refbox_xml_msg_creat: Error at xmlTextWriterEndDocument\n");

        return;

    }

 

    xmlFreeTextWriter(writer);

 

    fp = fopen(file, "w");

    if (fp == NULL) {

        printf("refbox_xml_msg_creat: Error at fopen\n");

        return;

    }

 

    fprintf(fp, "%s", (const char *) buf->content);

 

    fclose(fp);

}

___________________________________________________________________________________________________________________

 

 

this code reads from the file, i tryed to pass the buffer and chage to readfor memory, no success

 

 

 

[2]_____________________________________________________________________________________________________

void streamFile(const char *file,ref *ref_call_p){

    xmlTextReaderPtr reader;

    int ret;

        FILE *fp;

   /*xmlBufferPtr buf;*/

 

   reader = xmlReaderForFile(file, NULL, 0);

 

_______________________________________________________________________________________

 

 /* reader = xmlReaderForMemory(&buf, sizeof(buf), NULL, MY_ENCODING, 0);*/

 

_____________________________________________________________________________________ used this -> error

    if (reader != NULL) {

        ret = xmlTextReaderRead(reader);

        while (ret == 1) {

 

            processNode(reader, (ref*)ref_call_p);

            ret = xmlTextReaderRead(reader);

        }

        xmlFreeTextReader(reader);

        if (ret != 0) {

            fprintf(stderr, ": failed to parse\n");

        }

    } else {

        fprintf(stderr, "Unable to open \n");

    }

 

}

 

 

after this processnode is called, I will attach the code

 

if any one has an idea I would be very thanked

 

 

 

tks,

 

rodrigo

 

 

 

 

 

***********************************

 

Rodrigo de Meneses Barcelos Cabeceiras

Rua do Ameal nº.507 5ºEsq.

4200-061 Porto

Tel. 917776045

 

msn: rodrigo cabeceiras tvtel pt    

Web www.rodrigocabeceiras.pt.to

 

***********************************

 

Attachment: xml.zip
Description: Zip compressed data



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]