title: Insert new element to an XML file using SAX Filter
body: <p>I have an XMl file that looks like:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;game &gt;
    &lt;moves&gt;
      &lt;turn&gt;2&lt;/turn&gt;
      &lt;piece nr="1" /&gt;
      &lt;turn&gt;4&lt;/turn&gt;
      &lt;piece nr="1" /&gt;
  
    &lt;/moves&gt;
  &lt;/game&gt;
  </code></pre>
  
  <p>I am writing a Java program that takes the XML file as input then parses it with SAX and SAX filter and computes:</p>
  
  <ul>
  <li>the sum of the content of turn element (here=6)</li>
  <li>the number of piece elements (here=2) </li>
  </ul>
  
  <p>Then I want to use a SAX filter in order to generate an output XML file that are the same as the input one but with an additional element like:</p>
  
  <pre><code>&lt;s:statistics&gt;
      &lt;s:turn-total&gt;6&lt;/s:turn-total&gt;
      &lt;s:piece-count&gt;2&lt;/s:piece-count&gt;
  &lt;/s:statistics&gt;
  </code></pre>
  
  <p>The prefix <code>s</code> is a reference to a <a href="http://www.oniel.com/ns/test/" rel="nofollow">namespace</a>.</p>
  
  <p>My program so far is:</p>
  
  <pre><code> public class test{     
           public static void main(String[] args) throws Exception {
                  if (args.length != 2) {
                  System.err.println("error ");
                  System.exit(1);
                  }
                  String xmlInput = args[0];
                  String filteredXML = args[1];
                  test test1 = new test();
                      test1.sax(xmlInput, filteredXML);
              }
      private void sax(String gameXML, String filteredGameXML)throws Exception{
          FileInputStream fis = new FileInputStream( gameXML);
          InputSource is = new InputSource(fis);
          XMLReader xr = XMLReaderFactory.createXMLReader();
          XMLFilter xf = new MyFilter();
          xf.setParent(xr);
          xr = xf;
          xr.parse(is);
          xr.setFeature("http://xml.org/sax/features/namespaces", true);
          DefaultHandler handler = new DefaultHandler();
          xr.setContentHandler(handler);
      }
      private class MyFilter extends XMLFilterImpl{
               StringBuffer buffer;
           int temp=0;
               int sum=0;
               String ff;
               int numof=0;
               private MyFilter() {}
  
              @Override
                  public void startDocument() throws SAXException {
                       System.out.println( "START DOCUMENT" );
                      numof=0;        
                  }
  
                  public void startElement(String namespaceURI, String localName, String  name,   Attributes attributes) throws SAXException{
                      if(localName.equals("turn")){
                          buffer=new StringBuffer();
                          }
                      if("piece".equals(name)){
                          numof++;
                      }
                  }
  
                  public void characters(char[] ch, int start, int length) throws SAXException {
                      String s=new String(ch, start, length);
                      if(buffer!=null){
                          buffer.append(s);
                          }
                  }
  
                  public void endElement(String uri, String localName, String name)throws SAXException {
                      if(buffer!=null ){
                          ff=buffer.toString();
                          temp=Integer.valueOf(ff);
                          sum=sum+temp;
                          }
                          buffer=null;
                  }
                  public void endDocument() throws SAXException {
                      System.out.println( "END DOCUMENT" );
                      System.out.println("sum of turn: "+ sum);
                      System.out.println("sum of piece: "+ numof);
                  }
           }
  
      }
  </code></pre>
  
  <p>What should I do next?</p>