import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.AttributeList;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.HandlerBase;
import org.xml.sax.Parser;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


public class ParseXML {
	static Node parse(String fileName) 
	throws SAXException
		, IOException
		, ParserConfigurationException{
		return
		DocumentBuilderFactory
			.newInstance()
			.newDocumentBuilder()
			.parse(new File(fileName))
			.getDocumentElement();
	}

	static void saxParser(String fileName)
	throws ParserConfigurationException, SAXException, IOException{
		SAXParserFactory
			.newInstance()
			.newSAXParser()
			.parse(new File(fileName)
			, new DefaultHandler(){
				@Override
				public void startElement
				(String uri, String localName,
				String name, Attributes attributes) throws SAXException {
//					System.out.println(uri+"|"+localName+"|"+name);
				}
				@Override
				public void characters(char[] ch, int start, int length)
						throws SAXException {
					System.out.print(new String(ch,start,length));
				}
			});
	}
	
	static int count(Node n){
		int result=0;
		if (n.getNodeType()==Node.COMMENT_NODE)
			result=result+1;
		NodeList cs = n.getChildNodes();
		for (int i=0;i<cs.getLength();i++){
			result=result+count(cs.item(i));
		}
		return result;	
	}
	public static void main(String[] args) throws Exception, IOException, ParserConfigurationException {
		Node n = parse("/home/sep/fh/c/skript.xml");
		System.out.println(count(n));
		saxParser("/home/sep/fh/c/skript.xml");
	}
}
