[xml] reading data chunk by chunk



Hello

I am trying to parse data read from the network, so I progressively feed the data into a reader. I'm using a xmlReaderForMemory, when I receive the data I feed it using this function: xmlReaderNewMemory. For pulling the data out I use xmlTextReaderRead.
Is this the correct way to do such a thing ?

I'm using this xml sample. I'm feeding one line at a time, using different xmlReaderForMemory calls:
1.   "<stream1>"
2.   "<streams></streams>"
3.   "</stream1>\n";

I use the following code, slightly simplified for exemplification
"reader" is a class member initialised in ctor:

  char dummy = '<';
  reader = xmlReaderForMemory(&dummy, sizeof(dummy), NULL, NULL, 0);


void Parser::parse(
    const char* xmlData,
    unsigned int size
    ) throw (InvalidXmlException) {

    // initialize the parser
int result = xmlReaderNewMemory(reader, xmlData, size, NULL, NULL, /*XML_PARSE_NOERROR |*/ XML_PARSE_NOWARNING | XML_PARSE_RECOVER);
    if (result == -1)
      TRACEABLE_THROW(InvalidXmlException());

    result = xmlTextReaderRead(reader);
    if (result == -1)
      TRACEABLE_THROW(InvalidXmlException());

    while (result == 1) {   //second example
      xmlChar *name, *value;
      name = xmlTextReaderName(reader);
      if (name == NULL)
          name = xmlStrdup(BAD_CAST "--");
      value = xmlTextReaderValue(reader);

      printf("\n depth:%d type:%d NAME:%s empty:%d",
              xmlTextReaderDepth(reader),
              xmlTextReaderNodeType(reader),
              name,
              xmlTextReaderIsEmptyElement(reader));
      xmlFree(name);
      if (value == NULL)
          printf("\n");
      else {
          printf(" %s\n", value);
          xmlFree(value);
      }

      result = xmlTextReaderRead(reader);   //second example
if (result == -1) //second example
           TRACEABLE_THROW(InvalidXmlException()); //second example
    }              //second example
 }


After printing the properties of the first tag, <stream1>, I get this error:
 line 1: parser error : Extra content at the end of the document
<stream1>

In the second try I removed the lines having the comment //second example .
Thus calling just once xmlTextReaderRead.
Here, I get the properties of the tags (without the closing tag: </streams> ) until the reader reaches the last element, </stream>
I get this error:
Entity: line 1: parser error : StartTag: invalid element name
</stream1>
 ^

Any ideeas ?

Thank you very much.

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature



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