pulling with expat

Clark C. Evans clark.evans at manhattanproject.com
Sun Dec 19 22:43:08 GMT 1999


Does this code fragment help out?  It is far from
perfect, however, the "pull" based code would call 
getNextEvent() to return the next XML event...

    XML_Parser parser; 
    FILE        *fileptr; 
    struct Event { .... };
    Stack<Event> stack;

    void initialize(const char *filename) {
	fileptr = fopen(filename, ...);
        parser  = XML_ParserCreate(..);        
    }
   
    Event getNextEvent() {
      if(NULL==parser) 
         throw Error("Invalid Function Sequence");
      while(stack.isEmpty())
      {
        const int BUFSIZ = 4096;
        char  buff[BUFSIZ];
        // fill buffer with next chunk
        // of the file being read, make sure
        // that at least one new element is 
        // put on the stack..
        int len = fread(fileptr, ... );
	XML_Parse(parser, buff, len, (len!=BUFSIZ));      
        if(len!=BUFSIZ) 
          break;
      }
      if(!stack.isEmpty()) 
        return stack.pop();
      // if you get this far, we are at EOF
      fclose(fileptr);
      XML_ParserFree(parser);
      parser = NULL;
      fileptr = NULL;
      return NULL;
    }

    void StartElementHandler(void *userData,
                            const XML_Char *name,
                            const XML_Char **atts)
    { 
       // code to put event on the stack 
       newelements++;
    }


xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev at ic.ac.uk
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/ and on CD-ROM/ISBN 981-02-3594-1
To unsubscribe, mailto:majordomo at ic.ac.uk the following message;
unsubscribe xml-dev
To subscribe to the digests, mailto:majordomo at ic.ac.uk the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa at ic.ac.uk)





More information about the Xml-dev mailing list