title: XMLEventWriter throws javax.xml.stream.XMLStreamException: No open start element, when trying to write end element even though opened
body: <p>I have a application <code>JSON -&gt; XML</code> converter. This application will take a <code>List</code> of events that are converted to XML one by one. Before conversion, the header for the final XML will be created using the <code>start</code> method, and later converted events are added to <code>xmlEventWriter</code> one-by-one, and finally after all the conversion the closing tags are added to the XML using <code>end</code> method.</p>
  <p>I am facing an issue during the closing of tags and running into the error:</p>
  <pre><code>javax.xml.stream.XMLStreamException: No open start element, when trying to write end element
  </code></pre>
  <p>As per my understanding, everything is correct but still facing the issue don't know why.</p>
  <p>Following is the class that will create the <code>header body and closure</code> tags in XML:</p>
  <pre><code>
  
  public class EventXMLStreamCollector implements EventsCollector&lt;OutputStream&gt; {
  
      private final OutputStream stream;
      private final XMLEventWriter xmlEventWriter;
      private final XMLEventFactory events;
  
      public EventXMLStreamCollector(OutputStream stream) {
          this.stream = stream;
          try {
              xmlEventWriter = XMLOutputFactory.newInstance().createXMLEventWriter(stream);
              events = XMLEventFactory.newInstance();
          } catch (XMLStreamException e) {
              throw new EventFormatConversionException(&quot;Error occurred during the creation of XMLEventWriter : &quot; + e);
          }
  
      }
  
      public void collect(Object event) {
          System.out.println(&quot;COLLECT START&quot;);
          try {
              XMLEventReader xer = new EventReaderDelegate(XMLInputFactory.newInstance().createXMLEventReader(new StringReader(event.toString()))) {
                  @Override
                  public boolean hasNext() {
                      if (!super.hasNext())
                          return false;
                      try {
                          return !super.peek().isEndDocument();
                      } catch (XMLStreamException ignored) {
                          return true;
                      }
                  }
              };
              if (xer.peek().isStartDocument()) {
                  xer.nextEvent();
                  xmlEventWriter.add(xer);
              }
          } catch (XMLStreamException e) {
              throw new EventFormatConversionException(&quot;Error occurred during the addition of events to XMLEventWriter: &quot; + e);
          }
          System.out.println(&quot;COLLECT END&quot;);
      }
  
      @Override
      public OutputStream get() {
          return stream;
      }
  
      @Override
      public void start(Map&lt;String, String&gt; context) {
          System.out.println(&quot;START START&quot;);
          try {
              xmlEventWriter.add(events.createStartDocument());
              xmlEventWriter.add(events.createStartElement(new QName(&quot;doc:Document&quot;), null, null));
              xmlEventWriter.add(events.createNamespace(&quot;doc&quot;, &quot;urn:one&quot;));
              xmlEventWriter.add(events.createNamespace(&quot;xsi&quot;, &quot;http://www.w3.org/2001/XMLSchem-instance&quot;));
              xmlEventWriter.add(events.createNamespace(&quot;cbvmda&quot;, &quot;urn:two&quot;));
              
              for (Map.Entry&lt;String, String&gt; stringStringEntry : context.entrySet()) {
                  xmlEventWriter.add(events.createAttribute(stringStringEntry.getKey(), stringStringEntry.getValue()));
              }
  
              xmlEventWriter.add(events.createStartElement(new QName(&quot;Body&quot;), null, null));
              xmlEventWriter.add(events.createStartElement(new QName(&quot;EventList&quot;), null, null));
          } catch (XMLStreamException e) {
              throw new EventFormatConversionException(&quot;Error occurred during the creation of final XML file header information &quot; + e);
          }
          System.out.println(&quot;START END&quot;);
      }
  
      @Override
      public void end() {
          System.out.println(&quot;END START&quot;);
          try {
              System.out.println(xmlEventWriter.toString());
              xmlEventWriter.add(events.createEndElement(new QName(&quot;EventList&quot;), null));
              xmlEventWriter.add(events.createEndElement(new QName(&quot;Body&quot;), null));
              xmlEventWriter.add(events.createEndElement(new QName(&quot;doc:Document&quot;), null));
              xmlEventWriter.add(events.createEndDocument());
              xmlEventWriter.close();
          } catch (XMLStreamException e) {
              throw new EventFormatConversionException(&quot;Error occurred during the closing xmlEventWriter:&quot; + e);
          }
          System.out.println(&quot;END END&quot;);
      }
  
      @Override
      public void collectSingleEvent(Object event) {
          try {
              XMLEventReader xer = XMLInputFactory.newInstance().createXMLEventReader(new StringReader(event.toString()));
              if (xer.peek().isStartDocument()) {
                  xer.nextEvent();
              }
              xmlEventWriter.add(xer);
          } catch (XMLStreamException e) {
              System.out.println(&quot;ADDED : &quot; + e.getMessage());
              throw new EventFormatConversionException(&quot;Error occurred during the addition of events to XMLEventWriter: &quot; + e);
          }
      }
  }
  
  </code></pre>
  <p>I am getting the error for this line:</p>
  <pre><code>xmlEventWriter.add(events.createEndElement(new QName(&quot;Body&quot;), null));
  </code></pre>
  <p>I am not sure why I am getting this error. I am opening the <code>Body</code> tag and then trying to close it. I am sure that the flow is correct, I am calling <code>start</code>, <code>collect</code>, and then finally <code>end</code>. Following the <code>output</code> I am getting:</p>
  <pre><code>START START
  START END
  COLLECT START
  COLLECT END
  END START
  </code></pre>
  <p>I am not getting <code>END END</code> because of the error I am getting for the <code>closing of Body</code> tag. Can someone please help me understand this issue and provide some workaround and help.</p>