title: How to filter XML tags, then write filtered tags in dynamically generated files
body: <p>I Have this Java code:</p>
  
  <pre><code>public static void main(String[] args) throws Exception {
  
      String filename = "testXML.xml";
      String fileOutputName = "output.xml";
      try
      {           
          XMLInputFactory xmlif = null;
          xmlif = XMLInputFactory.newInstance();
          xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,Boolean.TRUE);
          xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,Boolean.FALSE);
          xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
          xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
          xmlif.setProperty(XMLInputFactory.REPORTER,"");
          FileInputStream fis = new FileInputStream(filename);
          XMLStreamReader xmlr = xmlif.createFilteredReader(xmlif.createXMLStreamReader(fis),new xmlReader());            
  
          OutputStream outputFile = new FileOutputStream(fileOutputName);                     
          XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();            
          XMLStreamWriter xmlWriter = outputFactory.createXMLStreamWriter(outputFile);    
  
          while (xmlr.hasNext())          
          {               
              write(xmlr, xmlWriter);             
              xmlr.next();
          }
          //write(xmlr, xmlWriter);                                   
          xmlWriter.flush();
          xmlWriter.close();          
          xmlr.close();
          outputFile.close();         
      }
      catch (Exception e)
      {
          e.printStackTrace();
      }
  }
  
  public boolean accept(XMLStreamReader reader) {     
      filters logic
      return true;
  }
  
  public static void write(XMLStreamReader xmlr, XMLStreamWriter writer) throws XMLStreamException {
      switch (xmlr.getEventType()) {
      case XMLEvent.START_ELEMENT:            
          final String localName = xmlr.getLocalName();
          writer.writeStartElement(localName);    
      break;
      case XMLEvent.END_ELEMENT:          
          writer.writeEndElement();
      break;
      case XMLEvent.SPACE:
      break;
      case XMLEvent.CHARACTERS:           
          writer.writeCharacters(xmlr.getTextCharacters(), xmlr.getTextStart(), xmlr.getTextLength());                    
          break;
      case XMLEvent.PROCESSING_INSTRUCTION:           
          writer.writeProcessingInstruction(xmlr.getPITarget(), xmlr.getPIData());            
      break;
      case XMLEvent.CDATA:                
              writer.writeCData(xmlr.getText());              
      break;
      case XMLEvent.COMMENT:
          writer.writeComment(xmlr.getText());
      break;
      case XMLEvent.ENTITY_REFERENCE:
          writer.writeEntityRef(xmlr.getLocalName());
      break;
      case XMLEvent.START_DOCUMENT:           
          String encoding = xmlr.getCharacterEncodingScheme();
          String version = xmlr.getVersion();
          if (encoding != null &amp;&amp; version != null)
              writer.writeStartDocument(encoding, version);
          else if (version != null)
              writer.writeStartDocument(xmlr.getVersion());               
      break;          
      case XMLEvent.END_DOCUMENT:         
          writer.writeEndDocument();          
      break;
      case XMLEvent.DTD:
          writer.writeDTD(xmlr.getText());            
      break;
      }
    }
  </code></pre>
  
  <p>which parses a XML like this:</p>
  
  <pre><code>&lt;xml&gt;
  &lt;element id="1"&gt;
      &lt;field&gt;Some field&lt;/field&gt;
      &lt;field2&gt;Some other field value&lt;/field2&gt;
      &lt;comment&gt;This will be ignored, so whatever.&lt;/comment&gt;
  &lt;/element&gt;
  &lt;element id="2"&gt;
      &lt;field&gt;Value for field 1&lt;/field&gt;
      &lt;field2&gt;Value for field 2&lt;/field2&gt;
      &lt;comment&gt;Blah, blah, blah&lt;/comment&gt;
  &lt;/element&gt;
  ...
  &lt;element id="n"&gt;
      &lt;field&gt;Value for field n&lt;/field&gt;
      &lt;field2&gt;Value for field n&lt;/field2&gt;
      &lt;comment&gt;Comment may be as long as a politician speech&lt;/comment&gt;
  &lt;/element&gt;&lt;/xml&gt;
  </code></pre>
  
  <p></p>
  
  <p>Basically I'm filtering the file by tags and writing the code in a XML File.</p>
  
  <p>Now, the trouble I'm having is when I try to do something like this:</p>
  
  <pre><code>while (xmlr.hasNext())          
          {
              XMLStreamWriter xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(new FileOutputStream(currentFile));
              write(xmlWriter);
              xmlr.next();
              xmlWriter.close();
          }           
          xmlr.close();
  </code></pre>
  
  <p>The currentFile is being changed elsewere, and when I run it, it gives me an exception saying that the first closing element couldn't be popped out. What I'm trying to do is filter tags, then when the tag changes write it in the file that was designed for that tag.</p>
  
  <p>This is the exception:</p>
  
  <pre><code>java.util.EmptyStackException
  at com.bea.xml.stream.util.Stack.pop(Stack.java:XX)
  at com.bea.xml.stream.XMLWriterBase.writeEndElement(XMLWriterBase.java:XX)
  at xmlReader.write(xmlReader.java:XX)
  at xmlReader.main(xmlReader.java:XX)
  </code></pre>
  
  <p>Does anybody know how can I fix that?</p>
  
  <p>Thanks.</p>
  
  <p><strong>Update 1</strong>
  Does anybody know if the implementation of XMLWriter deletes all the pushed values everytime that the writer is closed?</p>