[xml] xmlTextReaderNext skipping nodes



Ran into an issue when positioned at the end of an element, calling xmlTextReaderNext does not return the next node. This seems to only happen if the previous node had children and were traversed. I am not exactly sure of the states, but it looks like within xmlTextReaderNext, the XML_TEXTREADER_BACKTRACK state is not checked - as it looks like this is set if the node's children had been traversed and the end of the node has been hit.

Attached is a sample script that produces this issue (shows a case that works and one that doesn't). Only difference is that the second test accesses the element's child.

It looks like changing
if (reader->state == XML_TEXTREADER_END)
to
if (reader->state == XML_TEXTREADER_END || reader->state == XML_TEXTREADER_BACKTRACK) would resolve this issue, but I'm not completely sure of the states to say if this is correct or not.

Rob
#include <libxml/xmlreader.h>
#include <libxml/tree.h>

int main(void)
{
        xmlParserInputBufferPtr inputbfr;
        xmlTextReaderPtr reader;
        const xmlChar *strtest1 = "<r><n1></n1><n1></n1></r>";
        const xmlChar *strtest2 = "<r><n1>n1value</n1><n1></n1></r>";

        inputbfr = xmlParserInputBufferCreateMem(strtest1, xmlStrlen(strtest1), XML_CHAR_ENCODING_NONE);
        reader = xmlNewTextReader(inputbfr, NULL);
        xmlTextReaderRead(reader); /* Position at r */
        xmlTextReaderRead(reader); /* Position at n1 */
        xmlTextReaderRead(reader); /* Position at /n1 */
        printf("TEST1\n");
        if (xmlTextReaderNext(reader)) {
                printf("Node Found");
        } else {
                printf("Node Not Found\n");
        }
        xmlFreeParserInputBuffer(inputbfr);
        xmlFreeTextReader(reader);


        inputbfr = xmlParserInputBufferCreateMem(strtest2, xmlStrlen(strtest2), XML_CHAR_ENCODING_NONE);
        reader = xmlNewTextReader(inputbfr, NULL);
        xmlTextReaderRead(reader); /* Position at r */
        xmlTextReaderRead(reader); /* Position at n1 */
        xmlTextReaderRead(reader); /* Position at n1value */
        xmlTextReaderRead(reader); /* Position at /n1 */
        printf("\nTEST2\n");
        if (xmlTextReaderNext(reader)) {
                printf("Node Found");
        } else {
                printf("Node Not Found\n");
        }
        xmlFreeParserInputBuffer(inputbfr);
        xmlFreeTextReader(reader);


        return 0;
}


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