[xml] trying to do partial parsing

Date view Thread view Subject view Author view

From: James LewisMoss (jimdres@mindspring.com)
Date: Wed Jan 03 2001 - 23:01:58 EST


I'm in process of being subscribed to the list, but could you please
reply both to me and to the list? Thanks.

I'm trying to grab a part of a file and parse it as an xml element.
(I'd kinda like to have SAX like parsing, but when I find a particular
start tag just say "give me a tree from here to the end tag" at
certain points in the file.)

Anyway. After looking through the source for a long time I've got
this:

#include <gnome-xml/parser.h>
#include <gnome-xml/parserInternals.h>
#include <gnome-xml/tree.h>
#include <gnome-xml/SAX.h>
#include <gnome-xml/xmlIO.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <stdio.h>

#include <errno.h>

int
main(int argc, char** argv)
{
    char *filename;
    xmlParserCtxtPtr ctxt;
    xmlParserInputBufferPtr buf;
    xmlParserInputPtr input;
    int fd;
    xmlDocPtr doc;
    char inputbuf[1000];
    
    if(argc != 2)
    {
        printf("%s file.xml\n", argv[0]);
        exit(5);
    }

    filename = argv[1];

    fd = open(filename, O_RDONLY);
    printf("Contents of %s are:\n", filename);
    memset(inputbuf, 0, 1000);
    read(fd, inputbuf, 999);
    printf("%s\n\n", inputbuf);
    close(fd);

    xmlDefaultSAXHandlerInit();
    
    fd = open(filename, O_RDONLY);
    if(fd == -1)
    {
        printf("Error opening %s: %s\n", filename, strerror(errno));
        return 2;
    }
    
    ctxt = xmlNewParserCtxt();

    ctxt->sax = NULL;
    ctxt->userData = NULL;
    ctxt->directory = NULL;
    
    buf = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);

    input = xmlNewInputStream(ctxt);
    if (input == NULL)
    {
        xmlFreeParserCtxt(ctxt);
        return 1;
    }

    input->filename = NULL;
    input->buf = buf;
    input->base = input->buf->buffer->content;
    input->cur = input->buf->buffer->content;

    inputPush(ctxt, input);

    ctxt->instate = XML_PARSER_PROLOG;
    xmlParseMisc(ctxt);
    
    ctxt->instate = XML_PARSER_CONTENT;
    xmlParseElement(ctxt);

    ctxt->instate = XML_PARSER_EPILOG;
    xmlParseMisc(ctxt);
    
    if(!ctxt->wellFormed)
    {
        printf("Document is not well formed\n");
    }
    
    printf("Errno is: %d\n", ctxt->errNo);
    
    doc = ctxt->myDoc;
    
    xmlDocDump(stdout, doc);

    close(fd);

    doc = xmlParseFile(filename);

    printf("Doc is:\n");
    xmlDocDump(stdout, doc);
    
}

The read of the file works fine, and the xmlParseFile works fine, but
the middle part gives me nothing.

The file I'm testing on is:

<foobar>
 <bar/>
</foobar>

So. What am I missing?

Thanks
Jim

-- 
@James LewisMoss <dres@debian.org>      |  Blessed Be!
@    http://jimdres.home.mindspring.com |  Linux is kewl!
@"Argue for your limitations and sure enough, they're yours." Bach
----
Message from the list xml@rpmfind.net
Archived at : http://xmlsoft.org/messages/
to unsubscribe: echo "unsubscribe xml" | mail  majordomo@rpmfind.net


Date view Thread view Subject view Author view

This archive was generated by hypermail 2b29 : Wed Jan 03 2001 - 23:43:32 EST