数据加载中……
JAVA XML 解析之四 -SAX
2008年4月21日  Edited By DingDangXiaoMa
SAX 是采用事件驱动的方式与Dom最大的不同是,它不用读取完所有的XML文件就可以进行处理。采用流式的处理方式(呵,呵,我也不知道是什么意思)
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 = ""// 当前元素的数据
    private String lessonName = "";
    
private String lessonScore = "";

    
public MyContentHandler() {
    }

    
/**
     * 当其他某一个调用事件发生时,先调用此方法来在文档中定位。
     * 
     * 
@param locator
     
*/
    
public void setDocumentLocator(Locator locator) {

    }

    
/**
     * 在解析整个文档开始时调用
     * 
     * 
@throws SAXException
     
*/
    
public void startDocument() throws SAXException {
        System.out.println(
"**** Student information start ****");
    }

    
/**
     * 在解析整个文档结束时调用
     * 
     * 
@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
     *            本地名,如student
     * 
@param qName
     *            原始名,如LIT: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 {

    }

    
/**
     * 在解析到处理指令时,调用此方法。 这些处理指令不包括XML的版权指令,它由解析器本身识别。
     * 
     * 
@param target
     * 
@param data
     * 
@throws SAXException
     
*/
    
public void processingInstruction(String target, String data)
            
throws SAXException {

    }

    
/**
     * 当未验证解析器忽略实体时调用此方法
     * 
     * 
@param name
     * 
@throws SAXException
     
*/
    
public void skippedEntity(String name) throws SAXException {

    }
错误管理器:MyErrorHandler.java
public class MyErrorHandler implements ErrorHandler {

    
public MyErrorHandler() {
    }

    
/**
     * XML的警告信息
     * 
     * 
@param exception
     * 
@throws SAXException
     
*/
    
public void warning(SAXParseException exception) throws SAXException {
        System.out.println(
"!!!WARNING!!!");
        System.out.println(exception.getLineNumber() 
+ ":("
                
+ exception.getSystemId() + ")" + exception.getMessage());
    }

    
/**
     * 不符合XML规范时调用此方法
     * 
     * 
@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() {
    }

    
/**
     * 当实体声明为不必解析的实体时调用此方法,比如NDATA类型。
     * 
     * 
@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);
    }

    
/**
     * 当处理符号声明时调用此方法,比如NOTATION。
     * 
     * 
@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方式处理:MySAXParser.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");
    }
    
/**
     * 解析文档XML文档的URI
     * 
@param fileURI
     
*/
    
private void parserXMLFile(String fileURI) {
        
try {
            
// 通过指定解析器的名称来动态加载解析器
            XMLReader parser = XMLReaderFactory
                    .createXMLReader(
"org.apache.xerces.parsers.SAXParser");
            
// 处理内容前要注册内容管理器
            parser.setContentHandler(new MyContentHandler());
            
// 处理错误前要注册错误管理器
            parser.setErrorHandler(new MyErrorHandler());
            
// 处理DTD前要注册DTD管理器
            parser.setDTDHandler(new MyDTDHandler());
            
// 打开解析器的验证
            parser.setFeature("http://xml.org/sax/features/validation"true);
            
// 开始解析文档
            parser.parse(fileURI);
        } 
catch (IOException ioe) {
            System.out.println(ioe.getMessage());
        } 
catch (SAXException saxe) {
            System.out.println(saxe.getMessage());
        }
    }
}
说明:
org.xml.sax.*,包含于jdk 中。
这个例子是 javaresearch 上的例子,我也没有弄明白是什么意思,呵。呵。谁若是看到了些贴,请回复,说一说您的看法。

posted on 2008-04-21 17:34 叮当小马 阅读(177) 评论(0)  编辑  收藏 所属分类: XML


只有注册用户登录后才能发表评论。


网站导航: