char c = readCh(); if (c != '\uffff') error("Only comments and PIs may appear after " + "the root element");
  public int next()    throws XMLStreamException  {    if (lookahead)      {        lookahead = false;        return event;      }    if (event == XMLStreamConstants.END_ELEMENT)      {        // Pop namespace context        if (namespaceAware)          namespaces.removeFirst();        // Pop base context        if (baseAware)          bases.removeFirst();      }    if (!startEntityStack.isEmpty())      {        String entityName = (String) startEntityStack.removeFirst();        buf.setLength(0);        buf.append(entityName);        event = XMLStreamConstants.START_ENTITY;        return event;      }    else if (!endEntityStack.isEmpty())      {        String entityName = (String) endEntityStack.removeFirst();        if (entityName == null)          return next();        buf.setLength(0);        buf.append(entityName);        event = XMLStreamConstants.END_ENTITY;        return event;      }    try      {        if (!input.initialized)          {            input.init();            if (inputStack.size() > 1)              {                if (tryRead(TEST_XML_DECL))                  readTextDecl();              }          }        //System.out.println("input="+input.name+" "+input.inputEncoding);        switch (state)          {          case CONTENT:            if (tryRead(TEST_END_ELEMENT))              {                readEndElement();                if (stack.isEmpty())                  state = MISC;                event = XMLStreamConstants.END_ELEMENT;              }            else if (tryRead(TEST_COMMENT))              {                readComment();                event = XMLStreamConstants.COMMENT;              }            else if (tryRead(TEST_PI))              {                readPI();                event = XMLStreamConstants.PROCESSING_INSTRUCTION;              }            else if (tryRead(TEST_CDATA))              {                readCDSect();                event = XMLStreamConstants.CDATA;              }            else if (tryRead(TEST_START_ELEMENT))              {                state = readStartElement();                event = XMLStreamConstants.START_ELEMENT;              }            else              {                // Check for character reference or predefined entity                mark(8);                char c = readCh();                if (c == '&')                  {                    c = readCh();                    if (c == '#')                      {                        reset();                        event = readCharData(null);                      }                    else                      {                        // entity reference                        reset();                        readCh(); // &                        readReference();                        String ref = buf.toString();                        String text = (String) PREDEFINED_ENTITIES.get(ref);                        if (text != null)                          {                            event = readCharData(text);                          }                        else if (replaceERefs)                          {                            expandEntity(ref); //report start-entity                            event = next();                          }                        else                          {                            event = XMLStreamConstants.ENTITY_REFERENCE;                          }                      }                  }                else                  {                    reset();                    event = readCharData(null);                  }              }            break;          case EMPTY_ELEMENT:            state = stack.isEmpty() ? MISC : CONTENT;            event = XMLStreamConstants.END_ELEMENT;            break;          case INIT: // XMLDecl?            if (tryRead(TEST_XML_DECL))              readXMLDecl();            event = XMLStreamConstants.START_DOCUMENT;            state = PROLOG;            break;          case PROLOG: // Misc* (doctypedecl Misc*)?            skipWhitespace();            if (doctype == null && tryRead(TEST_DOCTYPE_DECL))              {                readDoctypeDecl();                event = XMLStreamConstants.DTD;              }            else if (tryRead(TEST_COMMENT))              {                readComment();                event = XMLStreamConstants.COMMENT;              }            else if (tryRead(TEST_PI))              {                readPI();                event = XMLStreamConstants.PROCESSING_INSTRUCTION;              }            else if (tryRead(TEST_START_ELEMENT))              {                state = readStartElement();                event = XMLStreamConstants.START_ELEMENT;              }            else              {                char c = readCh();                error("no root element: +U"+Integer.toHexString(c));              }            break;          case MISC: // Comment | PI | S            skipWhitespace();            if (tryRead(TEST_COMMENT))              {                readComment();                event = XMLStreamConstants.COMMENT;              }            else if (tryRead(TEST_PI))              {                readPI();                event = XMLStreamConstants.PROCESSING_INSTRUCTION;              }            else              {                if (event == XMLStreamConstants.END_DOCUMENT)                  throw new NoSuchElementException();                event = XMLStreamConstants.END_DOCUMENT;              }            break;          default:            event = -1;          }        return event;      }    catch (IOException e)      {        XMLStreamException e2 = new XMLStreamException();        e2.initCause(e);        throw e2;      }  }