次のような XML ファイルがあります。
<?xml version="1.0" encoding="UTF-8"?>
<game >
  <moves>
    <turn>2</turn>
    <piece nr="1" />
    <turn>4</turn>
    <piece nr="1" />
  </moves>
</game>
XMLファイルを入力として受け取り、それをSAXおよびSAXフィルタで解析して計算するJavaプログラムを作成しています:
- ターン要素のコンテンツの合計 (ここでは = 6)
 - ピース要素の数 (ここでは = 2)
 
次に、SAX フィルターを使用して、入力ファイルと同じ出力 XML ファイルを生成しますが、次のような要素が追加されます。
<s:statistics>
    <s:turn-total>6</s:turn-total>
    <s:piece-count>2</s:piece-count>
</s:statistics>
プレフィックスは名前空間sへの参照です。
これまでの私のプログラムは次のとおりです。
 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);
                }
         }
    }
次に何をすべきですか?