SAX를 이용한 Java XML 샘플 코드

import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;

public class SaxSample {
public static void main(String[] args) {
// SAX를 준비한다.
SAXParser saxParser = null;
 
try {
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
saxParser = saxFactory.newSAXParser();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}

// 핸들러를 작성한다.
SaxCustomHandler saxHandler = new SaxCustomHandler();
 
try {
saxParser.parse(new FileInputStream("f:\\1.xml"), saxHandler);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

// SAX 핸들러 클래스
class SaxCustomHandler extends DefaultHandler {
@Override
public void startDocument() {
System.out.println("XML 문서가 시작 되었습니다.");
}