- 浏览: 141207 次
- 性别:
- 来自: 广州
最新评论
-
zfms:
写的不孬,有时间交流一下
Openfire Server presence(在线状态)消息处理流程 -
yangjianzhouctgu:
您好 可以请教您个问题吗
Aptana Ajax库插件---EXT2.0 -
tianlihu:
不错,比较有效解决了问题
IE6中用了float:left之后导致margin-left双倍边距的BUG解决方法 -
mzba520:
我想请问能不能把openfire部署到tomcat中去,就是把 ...
openfire 源代码研究一 (运行环境的搭建) -
eric_hwp:
...
部署Openfire源码
Configure Java APIs (SAX, DOM, dom4j, XOM) using JAXP 1.3 to validate XML Documents with DTD and Schema(s).
Many Java XML APIs provide mechanisms to validate XML documents, the JAXP API can be used for most of these XML APIs but subtle configuration differences exists. This article shows five ways of how to configure different Java APIs (including DOM, SAX, dom4j and XOM) using JAXP 1.3 for checking and validating XML with DTD and Schema(s).
Contents
- Setup (Error Handler, Namespace Aware, Input Document, XML Schema, DTD)
- Checking Wellformed-ness (DOM, SAX, dom4j, XOM)
- Validate using internal DTD (DOM, SAX, dom4j, XOM)
- Validate using internal XSD (DOM, SAX, dom4j, XOM)
- Validate using external Schema(s) (DOM, SAX, dom4j, XOM)
- Validate using internal DTD and external Schema(s) (DOM, SAX, dom4j, XOM)
- Conclusion (Sample Code, Resources)
Setup
All underlying examples can be compiled and executed using Java 5.0 (JAXP 1.3) or higher and make use of the following components and settings.
Error Handler
To report errors, it is necessary to provide an ErrorHandler to the underlying implementation. The ErrorHandler used for the examples is a very simple one which reports the error to System.out and continues until the XML document has been fully parsed or until a fatal-error has been reported.
public class SimpleErrorHandler implements ErrorHandler { public void warning(SAXParseException e) throws SAXException { System.out.println(e.getMessage()); } public void error(SAXParseException e) throws SAXException { System.out.println(e.getMessage()); } public void fatalError(SAXParseException e) throws SAXException { System.out.println(e.getMessage()); } }
Namespace Aware
Namespaces have been introduced to XML after the first specification of XML had received the official W3C Recommendation status. This is the reason why (most of the) XML parser implementations do not support XML Namespaces by default, to handle the validation of XML documents with namespaces correctly it is therefore necessary to configure the underlying parsers to provide support for XML Namespaces.
Input Document
The input document ("contacts.xml"
) that has been used for all the code examples is shown below.
<!DOCTYPE contacts SYSTEM "contacts.dtd"> <contacts xsi:noNamespaceSchemaLocation="contacts.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <contact title="citizen"> <firstname>Edwin</firstname> <lastname>Dankert</lastname> </contact> </contacts>
XML Schema
The XML Schema ("contacts.xsd"
) as defined below has been used in the code examples to validate the input document. The input document contains an extra attribute which has not been defined in the XML Schema, this shows that the XML Schema has been used for the validation. When using this XML Schema to validate the input XML document, the following error gets reported:
cvc-complex-type.3.2.2: Attribute 'title' is not allowed to appear in element 'contact'.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="contacts"> <xs:complexType> <xs:sequence> <xs:element ref="contact"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="contact"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:NCName"/> <xs:element name="lastname" type="xs:NCName"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
DTD
The DTD ("contacts.dtd"
) as defined below has been used in the code examples to validate the input document. To highlight that the DTD has been used for the validation, the title attribute in the input document has a value which is not allowed according to this DTD. When using this DTD to validate the input XML document, the following error gets reported:
Attribute "title" with value "citizen" must have a value from the list "MR MS MRS ".
<!ELEMENT contacts (contact*)> <!ATTLIST contacts xsi:noNamespaceSchemaLocation CDATA #IMPLIED> <!ATTLIST contacts xmlns:xsi CDATA #IMPLIED> <!ELEMENT contact (firstname,lastname)> <!ATTLIST contact title (MR|MS|MRS) "MS"> <!ELEMENT firstname (#PCDATA)> <!ELEMENT lastname (#PCDATA)>
Checking Wellformed-ness
Before a document can be called XML and not csv, simple text or any other format, it needs to support the basic rules as defined by the XML Recommendation, when it adheres to these rules it is said to be Wellformed XML.Code Fragments: DOM, SAX, dom4j, XOM
DOM
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new SimpleErrorHandler()); Document document = builder.parse(new InputSource("document.xml"));
SAX
SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(true); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(new SimpleErrorHandler()); reader.parse(new InputSource("document.xml"));
dom4j
SAXReader reader = new SAXReader(); reader.setValidation(false); reader.setErrorHandler(new SimpleErrorHandler()); reader.read("contacts.xml");
XOM
SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(true); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(new SimpleErrorHandler()); Builder builder = new Builder(reader); builder.build("contacts.xml");
Validate using internal DTD
Parse the input document using only the DTD (contacts.dtd
), as defined by the DOCTYPE in the input document, for validation.
Code Fragments: DOM, SAX, dom4j, XOM
DOM
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new SimpleErrorHandler()); Document document = builder.parse(new InputSource("document.xml"));
SAX
SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(new SimpleErrorHandler()); reader.parse(new InputSource("document.xml"));
dom4j
SAXReader reader = new SAXReader(); reader.setValidation(true); reader.setErrorHandler(new SimpleErrorHandler()); reader.read("contacts.xml");
XOM
SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(new SimpleErrorHandler()); Builder builder = new Builder(reader); builder.build("contacts.xml");
Validate using internal XSD
Parse the input document using only the XML Schema (contacts.xsd
), as defined by the noNamespaceSchemaLocation attribute in the input document, for validation.
Code Fragments: DOM, SAX, dom4j, XOM
DOM
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new SimpleErrorHandler()); Document document = builder.parse(new InputSource("document.xml"));
SAX
SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); SAXParser parser = factory.newSAXParser(); parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(new SimpleErrorHandler()); reader.parse(new InputSource("document.xml"));
dom4j
SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); SAXParser parser = factory.newSAXParser(); parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); SAXReader reader = new SAXReader(parser.getXMLReader()); reader.setValidation(true); reader.setErrorHandler(new SimpleErrorHandler()); reader.read("contacts.xml");
XOM
SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); SAXParser parser = factory.newSAXParser(); parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(new SimpleErrorHandler()); Builder builder = new Builder(reader); builder.build("contacts.xml");
Validate using external Schema
Parse the input document using the schema (contacts.xsd
), as defined externally by the source-code.
Code Fragments: DOM, SAX, dom4j, XOM
DOM
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(true); SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); factory.setSchema(schemaFactory.newSchema( new Source[] {new StreamSource("contacts.xsd")})); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new SimpleErrorHandler()); Document document = builder.parse(new InputSource("document.xml"));
SAX
SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(true); SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); factory.setSchema(schemaFactory.newSchema( new Source[] {new StreamSource("contacts.xsd")})); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(new SimpleErrorHandler()); reader.parse(new InputSource("document.xml"));
dom4j
SAXParserFactory factory = SAXParserFactory.newInstance(); SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); factory.setSchema(schemaFactory.newSchema( new Source[] {new StreamSource("contacts.xsd")})); SAXParser parser = factory.newSAXParser(); SAXReader reader = new SAXReader(parser.getXMLReader()); reader.setValidation(false); reader.setErrorHandler(new SimpleErrorHandler()); reader.read("contacts.xml");
XOM
SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(true); SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); factory.setSchema(schemaFactory.newSchema( new Source[] {new StreamSource("contacts.xsd")})); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(new SimpleErrorHandler()); Builder builder = new Builder(reader); builder.build("contacts.xml");
Validate using internal DTD and external Schema
Parse the input document using the schema (contacts.xsd
), as defined externally by the source-code and the DTD (contacts.dtd
), as defined by the DOCTYPE in the input document, for validation.
Code Fragments: DOM, SAX, dom4j, XOM
DOM
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); factory.setSchema(schemaFactory.newSchema( new Source[] {new StreamSource("contacts.xsd")})); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new SimpleErrorHandler()); Document document = builder.parse(new InputSource("document.xml"));
SAX
SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); factory.setSchema(schemaFactory.newSchema( new Source[] {new StreamSource("contacts.xsd")})); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(new SimpleErrorHandler()); reader.parse(new InputSource("document.xml"));
dom4j
SAXParserFactory factory = SAXParserFactory.newInstance(); SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); factory.setSchema(schemaFactory.newSchema( new Source[] {new StreamSource("contacts.xsd")})); SAXParser parser = factory.newSAXParser(); SAXReader reader = new SAXReader(parser.getXMLReader()); reader.setValidation(true); reader.setErrorHandler(new SimpleErrorHandler()); reader.read("contacts.xml");
XOM
SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); factory.setSchema(schemaFactory.newSchema( new Source[] {new StreamSource("contacts.xsd")})); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(new SimpleErrorHandler()); Builder builder = new Builder(reader); builder.build("contacts.xml");
Conclusion
Several mechanisms and XML APIs can be used to parse and validate XML, by using JAXP 1.3 the mechanism can mostly stay the same for these different APIs.
Sample Code
Download any of the archives to get the full source-code for the examples above.
The archives consist of the ./contacts.xml
input XML file, ./contacts.xsd
the XML Schema document, ./contacts.dtd
the DTD document and the source-code for the fragments above, located in the ./src
directory.
The archive also contains a number XML Hammer validation projects included in the ./xmlhammer-projects
directory. To be able to execute these XML Hammer projects, you will need to have the XML Hammer application installed. This can be downloaded from:
Resources
- Extensible Markup Language (XML) 1.0
http://www.w3.org/TR/REC-xml/ - Namespaces in XML
http://www.w3.org/TR/REC-xml-names/ - Java 5.0
http://java.sun.com/j2se/1.5.0/ - Java 6.0
http://java.sun.com/javase/6/ - JAXP
http://jcp.org/en/jsr/detail?id=206 - dom4j
http://www.dom4j.org/ - XOM
http://www.xom.nu/ - JDOM
http://www.jdom.org/
发表评论
-
flex 电子书籍下载
2009-11-25 17:12 2144http://wmcai.blog.163.com/blog/ ... -
基于servlet导出Flex/Flash界面为图形文件的简单方法
2009-07-31 14:12 1655关键字: flex,chart,java 使用fl ... -
Flex 3的本地化应用
2009-07-20 13:30 1558这两天看了一下Flex 3的 ... -
FLEX组件的生命周期
2009-05-22 14:42 1648组件实例化生命周期描述了用组件类创建组件对象时所发生的一系列步 ... -
Flex性能优化之cpu占用率
2009-05-08 10:39 4830先扯几句,team中有一个正在试运行的项目,最近被客户打了 ... -
Understanding garbage collection in Flash Player 9
2009-05-06 19:19 1196原文 Understanding garbage ... -
关于firefox文本折行的问题
2009-04-13 14:57 1593一直有传说这个是firefox的一个bug,属于无法完美解决的 ... -
Adobekit教你如何修改FLASH右键菜单
2009-04-07 10:32 4904有些东西只有用到了才 ... -
CSS IE7 IE6 Firefox多浏览器兼容-著名的星号* Html Hack(招数)[z]
2009-04-01 10:08 2878From: [url]http://www.keephelp. ... -
Flex上载和下载文件
2009-01-20 14:28 5689示例:上载和下载文件 FileIO 示例说明了在 Flash ... -
Flex 2 中的元数据标签
2009-01-19 16:33 906原文作者:Rich Tretola(作者是everything ... -
理解ClassLoader机制
2009-01-18 23:09 1221当JVM(Java虚拟机)启动时,会形成由三个类加载器组成的初 ... -
一个简易实用的web权限管理模块的应用与实现
2009-01-18 21:59 1401本文介绍一个简易实用的web权限管理模块的应用与实现。 ... -
JS中Null与Undefined的区别
2009-01-04 17:40 2353在JavaScript中存在这样两种原始类型:Null与Und ... -
Flex3 framework RSL机制介绍
2008-12-25 17:45 2397阻碍Flex应用的一个很大因素就是采用Flex框架的程序体积非 ... -
IE6中用了float:left之后导致margin-left双倍边距的BUG解决方法
2008-12-22 17:17 5033先看css代码: div { float:left; ... -
Adobe Meermeer:跨浏览器网页测试工具
2008-12-09 09:24 1663每年Adobe公司都会有一个最具吸引力的项目,在2007年的会 ... -
网站性能优化
2008-12-03 17:59 971/** *作者:张荣华 *日期:2008-12-01 **/ ... -
openfire 源代码研究一 (运行环境的搭建)
2008-11-30 21:31 3320关于Openfire的介绍在此不多说了,网上有很多关于这个基于 ... -
FLEX3中应用CSS完全详解手册!
2008-11-21 14:13 1820编辑完这个FLEX下的CSS说明后,我基本已经兵临崩溃边缘了。 ...
相关推荐
side your compiled code, I have always preferred simple abstraction either using roles and their corresponding mappings in the database or using simple xml file to store action to role mappings. ...
As well as focusing on client-side JavaScript, you will also learn how to work with the Browser Object Model, the Document Object Model (DOM), how to use XML and JSON as well as communicate with ...
As well as focusing on client-side JavaScript, you will also learn how to work with the Browser Object Model, the Document Object Model (DOM), how to use XML and JSON as well as communicate with ...
<END><br>12,ASP_CDOMail.zip Using Microsoft Technologies, a simple example of how to send email using ASP, CDO, and IIS. Full Source. Information on CDO is well documented on the Internet. <END>...
最后,提供的PDF文件“`How-do-I-validate-if-a-XML-tag-missing-or-using-po.pdf`”可能包含了更详细的步骤和示例,以供进一步学习和参考。确保阅读这份文档以获取更全面的理解和解决方案。在实际应用中,根据具体...
CustEditXML.zip Complete VB application that retrieves customer information from an XML script, allows you to make changes to the data, and saves the record using other XML scripts. This is a ...
and validate user input •Use OOP techniques, including encapsulation and method overloading •Build custom controls and .NET assemblies •Access and modify data using XML, Microsoft ADO...
2. **Step 2**: Use checkpoints or assertions to validate the actual response against the expected one. 3. **Step 3**: Review the verification results for any discrepancies. 4. **Step 4**: Adjust the ...
PEP 529: Change Windows filesystem encoding to UTF-8 PEP 528: Change Windows console encoding to UTF-8 PEP 520: Preserving Class Attribute Definition Order PEP 468: Preserving Keyword Argument ...
14.5.4. Using Metacommands to Control Filter or Validator Rules 14.5.4.1. The FIELDS metacommand 14.5.4.2. The PRESENCE metacommand 14.5.4.3. The DEFAULT_VALUE metacommand 14.5.4.4. The ALLOW_...