鍛?鍛碉紝鎴戜篃涓嶇煡閬撴槸浠涔堟剰鎬?/span>)
example:
娉ㄥ唽鍐呭綆$悊鍣細MyContentHandler.java
public class MyContentHandler implements ContentHandler {
// DTD涓畾涔夌殑鍏冪礌
private static final String ELEMENT_NAME = "name";
private static final String ELEMENT_SEX = "sex";
private static final String ELEMENT_LESSON = "lesson";
private static final String ELEMENT_LESSON_NAME = "lessonName";
private static final String ELEMENT_LESSON_SCORE = "lessonScore";
private static final String ELEMENT_STUDENT = "student";
private static final String ELEMENT_LINE = "breakLine";
private String currentData = ""; // 褰撳墠鍏冪礌鐨勬暟鎹?/span>
private String lessonName = "";
private String lessonScore = "";
public MyContentHandler() {
}
/**
* 褰撳叾浠栨煇涓涓皟鐢ㄤ簨浠跺彂鐢熸椂錛屽厛璋冪敤姝ゆ柟娉曟潵鍦ㄦ枃妗d腑瀹氫綅銆?br />
*
* @param locator
*/
public void setDocumentLocator(Locator locator) {
}
/**
* 鍦ㄨВ鏋愭暣涓枃妗e紑濮嬫椂璋冪敤
*
* @throws SAXException
*/
public void startDocument() throws SAXException {
System.out.println("**** Student information start ****");
}
/**
* 鍦ㄨВ鏋愭暣涓枃妗g粨鏉熸椂璋冪敤
*
* @throws SAXException
*/
public void endDocument() throws SAXException {
System.out.println("**** Student information end ****");
}
/**
* 鍦ㄨВ鏋愬悕瀛楃┖闂村紑濮嬫椂璋冪敤
*
* @param prefix
* @param uri
* @throws SAXException
*/
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
}
/**
* 鍦ㄨВ鏋愬悕瀛楃┖闂寸粨鏉熸椂璋冪敤
*
* @param prefix
* @throws SAXException
*/
public void endPrefixMapping(String prefix) throws SAXException {
}
/**
* 鍦ㄨВ鏋愬厓绱犲紑濮嬫椂璋冪敤
*
* @param namespaceURI
* @param localName
* @param qName
* @param atts
* @throws SAXException
*/
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
}
/**
* 鍦ㄨВ鏋愬厓绱犵粨鏉熸椂璋冪敤
*
* @param namespaceURI
* @param localName
* 鏈湴鍚嶏紝濡俿tudent
* @param qName
* 鍘熷鍚嶏紝濡侺IT:student
* @throws SAXException
*/
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
if (localName.equals(ELEMENT_NAME)) {
System.out.println(localName + ":" + currentData);
}
if (localName.equals(ELEMENT_SEX)) {
System.out.println(localName + ":" + currentData);
}
if (localName.equals(ELEMENT_LESSON_NAME)) {
this.lessonName = currentData;
}
if (localName.equals(ELEMENT_LESSON_SCORE)) {
this.lessonScore = currentData;
}
if (localName.equals(ELEMENT_LESSON)) {
System.out.println(lessonName + ":" + lessonScore);
}
if (localName.equals(ELEMENT_LINE)) {
System.out.println("------------------------------------");
}
}
/**
* 鍙栧緱鍏冪礌鏁版嵁
*
* @param ch
* @param start
* @param length
* @throws SAXException
*/
public void characters(char[] ch, int start, int length)
throws SAXException {
currentData = new String(ch, start, length).trim();
}
/**
* 鍙栧緱鍏冪礌鏁版嵁涓殑絀虹櫧
*
* @param ch
* @param start
* @param length
* @throws SAXException
*/
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException {
}
/**
* 鍦ㄨВ鏋愬埌澶勭悊鎸囦護鏃訛紝璋冪敤姝ゆ柟娉曘?nbsp;榪欎簺澶勭悊鎸囦護涓嶅寘鎷琗ML鐨勭増鏉冩寚浠わ紝瀹冪敱瑙f瀽鍣ㄦ湰韜瘑鍒?br />
*
* @param target
* @param data
* @throws SAXException
*/
public void processingInstruction(String target, String data)
throws SAXException {
}
/**
* 褰撴湭楠岃瘉瑙f瀽鍣ㄥ拷鐣ュ疄浣撴椂璋冪敤姝ゆ柟娉?br />
*
* @param name
* @throws SAXException
*/
public void skippedEntity(String name) throws SAXException {
}
閿欒綆$悊鍣細MyErrorHandler.java
public class MyErrorHandler implements ErrorHandler {
public MyErrorHandler() {
}
/**
* XML鐨勮鍛婁俊鎭?br />
*
* @param exception
* @throws SAXException
*/
public void warning(SAXParseException exception) throws SAXException {
System.out.println("!!!WARNING!!!");
System.out.println(exception.getLineNumber() + ":("
+ exception.getSystemId() + ")" + exception.getMessage());
}
/**
* 涓嶇鍚圶ML瑙勮寖鏃惰皟鐢ㄦ鏂規硶
*
* @param exception
* @throws SAXException
*/
public void error(SAXParseException exception) throws SAXException {
System.out.println("!!!ERROR!!!");
System.out.println(exception.getLineNumber() + ":("
+ exception.getSystemId() + ")" + exception.getMessage());
}
/**
* 闈炶壇鏋勭殑鏂囨。鏃惰皟鐢ㄦ鏂規硶
*
* @param exception
* @throws SAXException
*/
public void fatalError(SAXParseException exception) throws SAXException {
System.out.println("!!!FATAL!!!");
System.out.println(exception.getLineNumber() + ":("
+ exception.getSystemId() + ")" + exception.getMessage());
}
}
DTD 綆$悊鍣細 MyDTDHandler.java
public class MyDTDHandler implements DTDHandler {
public MyDTDHandler() {
}
/**
* 褰撳疄浣撳0鏄庝負涓嶅繀瑙f瀽鐨勫疄浣撴椂璋冪敤姝ゆ柟娉曪紝姣斿NDATA綾誨瀷銆?br />
*
* @param name
* @param publicId
* @param systemId
* @throws SAXException
*/
public void notationDecl(String name, String publicId, String systemId)
throws SAXException {
System.out.println("**notationDecl**");
System.out.println("name:" + name);
System.out.println("publicId" + publicId);
System.out.println("systemId:" + systemId);
}
/**
* 褰撳鐞嗙鍙峰0鏄庢椂璋冪敤姝ゆ柟娉曪紝姣斿NOTATION銆?br />
*
* @param name
* @param publicId
* @param systemId
* @param notationName
* @throws SAXException
*/
public void unparsedEntityDecl(String name, String publicId,
String systemId, String notationName) throws SAXException {
System.out.println("**unparsedEntityDecl**");
System.out.println("name:" + name);
System.out.println("publicId" + publicId);
System.out.println("systemId:" + systemId);
System.out.println("notationName:" + notationName);
}
}
SAX鏂瑰紡澶勭悊錛歁ySAXParser.java
import java.io.IOException;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
public class MySAXParser {
public MySAXParser() {
}
public static void main(String[] args) {
MySAXParser mySAXParser = new MySAXParser();
mySAXParser.parserXMLFile("http://localhost/struts2.0/xml/SutInfo.xml");
}
/**
* 瑙f瀽鏂囨。XML鏂囨。鐨刄RI
* @param fileURI
*/
private void parserXMLFile(String fileURI) {
try {
// 閫氳繃鎸囧畾瑙f瀽鍣ㄧ殑鍚嶇О鏉ュ姩鎬佸姞杞借В鏋愬櫒
XMLReader parser = XMLReaderFactory
.createXMLReader("org.apache.xerces.parsers.SAXParser");
// 澶勭悊鍐呭鍓嶈娉ㄥ唽鍐呭綆$悊鍣?/span>
parser.setContentHandler(new MyContentHandler());
// 澶勭悊閿欒鍓嶈娉ㄥ唽閿欒綆$悊鍣?/span>
parser.setErrorHandler(new MyErrorHandler());
// 澶勭悊DTD鍓嶈娉ㄥ唽DTD綆$悊鍣?/span>
parser.setDTDHandler(new MyDTDHandler());
// 鎵撳紑瑙f瀽鍣ㄧ殑楠岃瘉
parser.setFeature("http://xml.org/sax/features/validation", true);
// 寮濮嬭В鏋愭枃妗?/span>
parser.parse(fileURI);
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
} catch (SAXException saxe) {
System.out.println(saxe.getMessage());
}
}
}
璇存槑錛?br />
org.xml.sax.*,鍖呭惈浜巎dk 涓?br />
榪欎釜渚嬪瓙鏄?javaresearch 涓婄殑渚嬪瓙錛屾垜涔熸病鏈夊紕鏄庣櫧鏄粈涔堟剰鎬濓紝鍛點傚懙銆傝皝鑻ユ槸鐪嬪埌浜嗕簺璐達紝璇峰洖澶嶏紝璇翠竴璇存偍鐨勭湅娉曘?br />

]]>