/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999,2000,2001 The Apache Software Foundation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2001, International
* Business Machines, Inc., http://www.apache.org. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package xni;
import org.apache.xerces.impl.Constants;
import org.apache.xerces.impl.XMLNamespaceBinder;
import org.apache.xerces.util.SymbolTable;
import org.apache.xerces.util.DefaultErrorHandler;
import org.apache.xerces.util.MessageFormatter;
import org.apache.xerces.util.XMLAttributesImpl;
//for testing
import org.apache.xerces.impl.xs.XSTypeDecl;
import org.apache.xerces.impl.xs.ElementPSVImpl;
import org.apache.xerces.impl.xs.AttributePSVImpl;
import org.apache.xml.serialize.IndentPrinter;
import org.apache.xml.serialize.EncodingInfo;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.Printer;
import org.apache.xml.serialize.LineSeparator;
import org.apache.xerces.xni.QName;
import org.apache.xerces.xni.XMLString;
import org.apache.xerces.xni.XMLLocator;
import org.apache.xerces.xni.XNIException;
import org.apache.xerces.xni.NamespaceContext;
import org.apache.xerces.xni.XMLAttributes;
import org.apache.xerces.xni.Augmentations;
import org.apache.xerces.xni.XMLDocumentHandler;
import org.apache.xerces.xni.psvi.ItemPSVI;
import org.apache.xerces.xni.psvi.ElementPSVI;
import org.apache.xerces.xni.psvi.AttributePSVI;
import org.apache.xerces.xni.parser.XMLComponent;
import org.apache.xerces.xni.parser.XMLComponentManager;
import org.apache.xerces.xni.parser.XMLDocumentFilter;
import org.apache.xerces.xni.parser.XMLConfigurationException;
import org.apache.xerces.xni.parser.XMLErrorHandler;
import org.apache.xerces.xni.parser.XMLInputSource;
import org.apache.xerces.xni.parser.XMLParseException;
import java.io.*;
import java.util.*;
/**
* This class is a intersepts XNI events and serialized
* XML infoset and Post Schema Validation Infoset.
*
* @author Arun Yadav,Sun Miscrosystem.
* @version $Id: PSVIWriter.java,v 1.3 2001/12/17 17:50:21 elena Exp $
*/
public class PSVIWriter
implements XMLComponent, XMLDocumentFilter {
public static final String XERCES_PSVI_NS = "http://apache.org/xml/2001/PSVInfosetExtension";
/** Property identifier: Namespace Binder */
protected static final String NAMESPACE_BINDER =
Constants.XERCES_PROPERTY_PREFIX + Constants.NAMESPACE_BINDER_PROPERTY;
/** Property identifier: symbol table. */
protected static final String SYMBOL_TABLE =
Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
/** Feature id: include ignorable whitespace. */
protected static final String INCLUDE_IGNORABLE_WHITESPACE =
"http://apache.org/xml/features/dom/include-ignorable-whitespace";
protected static final String PSVI_OUTPUT ="psvi_output.xml";
/** Include ignorable whitespace. */
protected boolean fIncludeIgnorableWhitespace;
/** Recognized features. */
protected static final String[] RECOGNIZED_FEATURES = {
NAMESPACE_BINDER,
INCLUDE_IGNORABLE_WHITESPACE,
// PSVINFOSET,
};
/** Recognized properties. */
protected static final String[] RECOGNIZED_PROPERTIES={
SYMBOL_TABLE,
};
/** PSVInfoset */
protected boolean fPSVInfoset;
/** Symbol: "". */
private String fEmptySymbol;
/** Symbol: "xml". */
private String fXmlSymbol;
/** Symbol: "xmlns". */
private String fXmlnsSymbol;
/** XMLNS namespace: XML-Infoset */
public static final String XMLNS_URI ="http://www.w3.org/2000/xmlns/";
/** Document handler. */
protected XMLDocumentHandler fDocumentHandler;
/** Symbol table. */
protected SymbolTable fSymbolTable;
/** NamespaceBinder*/
protected XMLNamespaceBinder fNamespaceBinder;
/** Attribute QName. */
private QName fAttrQName = new QName();
/** Attributes and Element Info is cached in stack */
private Stack _elementState =new Stack();
/** The output stream. */
private OutputStream _output;
/** The underlying writer. */
private java.io.Writer _writer;
private EncodingInfo _encodingInfo;
private final StringBuffer fErrorBuffer = new StringBuffer();
/** The printer used for printing text parts. */
protected Printer _printer;
public PSVIWriter() {
System.out.println("Generating Schema Information Set Contribution (PSVI) \n"
+ "which follow as a consequence of validation and/or assessment.");
System.out.println("NOTE: Requires use of -s and -v");
System.out.println("Output: generated in "+PSVI_OUTPUT);
} // <init>()
//REVISIT
// 1. where to output the PSVI info to user( output console or file)?
// 2. Is there any other better way to format the output.
public void reset(XMLComponentManager componentManager)
throws XNIException {
// Feature's name for PSVIWriter is not yet decided.
/* try {
fPSVInfoset = componentManager.getFeature(PSVINFOSET);
}
catch (XMLConfigurationException e) {
fPSVInfoset = false;
}*/
/**For Testing */
fPSVInfoset = true;
fNamespaceBinder = (XMLNamespaceBinder)componentManager.getProperty(NAME
Xerces-J-bin.2.0.0.beta4.zip
需积分: 0 195 浏览量
更新于2024-04-20
收藏 4.22MB ZIP 举报
《Xerces-J-bin.2.0.0.beta4.zip:深入理解SAX XML解析器》
在IT领域,XML(可扩展标记语言)作为数据交换和存储的标准,被广泛应用于各种应用程序之间。为了处理XML文档,解析器是不可或缺的一部分。本文将详细探讨Xerces-J,一个著名的Java XML解析器,特别是其2.0.0.beta4版本,并重点关注SAX(Simple API for XML)解析方式。
Xerces-J是由Apache软件基金会开发的一个开源项目,它是Java平台上的一个强大的XML处理库。Xerces-J以其高性能、稳定性和广泛的XML标准支持而受到赞誉。2.0.0.beta4是该解析器的一个早期版本,虽然可能不如最新版功能丰富,但对于研究和了解XML解析的原理,这个版本同样具有参考价值。
SAX是一种基于事件驱动的XML解析模型,与DOM(文档对象模型)解析器不同。DOM将整个XML文档加载到内存中形成一棵树形结构,允许开发者遍历和修改整个文档。而SAX则采用流式处理,它不构建完整的文档对象模型,而是以事件的形式通知用户XML文档的结构和内容。这种设计使得SAX解析器在处理大型XML文件时,内存消耗远低于DOM,特别适合资源有限的环境。
在Xerces-J中,SAX解析器通过实现`org.xml.sax.Parser`或`org.xml.sax.XMLReader`接口来提供服务。当解析XML文件时,解析器会触发一系列事件,如开始文档、开始元素、字符数据、结束元素等,开发者可以注册事件处理器(`ContentHandler`和`DTDHandler`等)来响应这些事件,从而实现对XML数据的处理。
在使用Xerces-J的SAX解析器时,有以下几点需要注意:
1. **配置解析器**:你可以通过`XMLReaderFactory`类创建并配置XMLReader实例,设置解析器的行为,例如是否开启命名空间支持,或者指定错误处理程序。
2. **注册事件处理器**:通过`setContentHandler`方法设置ContentHandler,处理元素和文本内容;通过`setDTDHandler`方法设置DTDHandler,处理文档类型定义。
3. **开始解析**:调用`parse`方法,传入XML输入源(如文件、URL或字节数组),解析器开始工作,触发事件处理器中的相应方法。
4. **错误处理**:SAX解析器会在遇到错误时调用`ErrorHandler`接口的方法,开发者应妥善处理这些错误,确保程序的健壮性。
Xerces-J还提供了DOM解析器,以及XML Schema和XML DTD的支持,使得开发者可以根据实际需求选择合适的解析方式。在处理大型XML文档时,SAX解析器由于其轻量级和高效性,通常成为首选。然而,如果需要频繁地查询和修改XML结构,DOM解析器的便利性可能会更胜一筹。
Xerces-J 2.0.0.beta4版本是了解和学习XML解析的好工具,尤其是对于SAX解析模型的掌握。结合实际项目需求,开发者可以灵活运用Xerces-J提供的API,有效地处理XML数据,提高程序的性能和效率。
段子手-168
- 粉丝: 4809
- 资源: 2745
最新资源
- MySQL基础-自然语言全文搜索.pdf
- 北大纵横—中国城市设计西南分院—0820培训工作计划.doc
- 北大纵横—中国城市设计西南分院—1801培训控制程序.doc
- 北大纵横—中国城市设计西南分院—北大纵横培训——动荡期的企业增长盈利模式.ppt
- 北大纵横—中国城市设计西南分院—培训工作计划-xf.doc
- 北大纵横—中国城市设计西南分院—项目管理培训教材(66P).ppt
- 北海烟草人力资源咨询—北海烟草人力资源管理咨询培训计划(第二阶段).doc
- mysql安装配置教程.txt
- 北海烟草人力资源咨询—培训管理流程.doc
- 北海烟草人力资源咨询—北海烟草人力资源培训计划.doc
- 北海烟草人力资源咨询—培训管理流程〜.doc
- 北海烟草人力资源咨询—人力资源资源培训计划.doc
- 北海烟草人力资源咨询—现代人力资源管理培训课程安排.doc
- mysql安装配置教程.txt
- 博思智联-三联集团-培训汇总表1-8.doc
- 博思智联-三联集团-评估培训.ppt