Validate XML using a XSD (XML Schema)
Consider this XML file howto.xml :
<?xml version="1.0" encoding="ISO-8859-1"?>
<howto xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<topic>
<title>Java</title>
<url>http://www.rgagnon.com/topics/java-xml.html</url>
</topic>
<topic>
<title>PowerBuilder</title>
<url>http://www.rgagnon.com/topics/pb-powerscript.htm</url>
</topic>
<topic>
<title>Javascript</title>
<url>http://www.rgagnon.com/topics/js-language.html</url>
</topic>
<topic>
<title>VBScript</title>
<url>http://www.rgagnon.com/topics/wsh-vbs.html</url>
</topic>
</howto>
The external howto.xsd :
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="howto">
<xs:complexType>
<xs:sequence>
<xs:element name="topic" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="url" type="httpURI"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element> <xs:simpleType name="httpURI">
<xs:restriction base="xs:anyURI">
<xs:pattern value="http://.*" />
</xs:restriction>
</xs:simpleType> </xs:schema>
The code (using SAX parser) to validate an XML file using a given external XSD.
import java.io.IOException; // SAX
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.XMLReader; //SAX and external XSD
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.SchemaFactory; import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.InputSource; public class XMLUtils { private XMLUtils() {} // validate SAX and external XSD
public static boolean validateWithExtXSDUsingSAX(String xml, String xsd)
throws ParserConfigurationException, IOException
{
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(true); SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
SAXParser parser = null;
try {
factory.setSchema(schemaFactory.newSchema(new Source[] {new StreamSource( xsd )}));
parser = factory.newSAXParser();
}
catch (SAXException se) {
System.out.println("SCHEMA : " + se.getMessage()); // problem in the XSD itself
return false;
} XMLReader reader = parser.getXMLReader();
reader.setErrorHandler(
new ErrorHandler() {
public void warning(SAXParseException e) throws SAXException {
System.out.println("WARNING: " + e.getMessage()); // do nothing
} public void error(SAXParseException e) throws SAXException {
System.out.println("ERROR : " + e.getMessage());
throw e;
} public void fatalError(SAXParseException e) throws SAXException {
System.out.println("FATAL : " + e.getMessage());
throw e;
}
}
);
reader.parse(new InputSource(xml));
return true;
}
catch (ParserConfigurationException pce) {
throw pce;
}
catch (IOException io) {
throw io;
}
catch (SAXException se){
return false;
}
} public static void main (String args[]) throws Exception{
System.out.println
(XMLUtils.validateWithExtXSDUsingSAX
("howto.xml", "howto.xsd"));
/*
output :
true
*/
}
}
The XML can contain a reference to the XSD to be used.
<?xml version="1.0" encoding="ISO-8859-1"?>
<howto xsi:noNamespaceSchemaLocation="howto.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<topic>
<title>Java</title>
<url>http://www.rgagnon.com/topics/java-xml.html</url>
</topic>
...
</howto>
The code (using DOM parser) to validate an XML file using the referenced XSD :
Validate XML using a XSD (XML Schema)的更多相关文章
- 如何将XML转换成XSD(XML Schema)文件
将xml装换为xsd,先决条件是已经安装了Visual Stutio 1) 输入cmd在运行窗口 2) 将xsd的路径加入到path变量 set path=%path%;C:\Program File ...
- XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式
XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD),作用是定义 XML 文档的合法构建模块,类似 DTD,但更加强大. 作用有: ①定义 ...
- XSD(XML Schema Definition)学习笔记
今天学习了XSD相关的知识,为了以后查找的方便,写一些笔记. 一.什么是XSD? 1.XSD全称:XML Schema Definition.XML Schema 的作用是定义 XML 文档的合法构建 ...
- 28.XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式
转自https://www.cnblogs.com/gdjlc/archive/2013/09/08/3308229.html XML Schema 语言也称作 XML Schema 定义(XML S ...
- MyEclipse XML & XML架构教程:XML Schema (XSD)编辑器
[MyEclipse CI 2019.4.0安装包下载] 1. MyEclipse中的XSD编辑 本文档介绍MyEclipse XML Schema(XSD)编辑器中的一些可用函数.XML Schem ...
- XmlValidationHelper XSD、Schema(XmlSchemaSet)、XmlReader(XmlValidationSettings)、XmlDocument、XDocument Validate
namespace Test { using Microshaoft; using System; using System.Xml; using System.Xml.Linq; class Pro ...
- xml语法、DTD约束xml、Schema约束xml、DOM解析xml
今日大纲 1.什么是xml.xml的作用 2.xml的语法 3.DTD约束xml 4.Schema约束xml 5.DOM解析xml 1.什么是xml.xml的作用 1.1.xml介绍 在前面学习的ht ...
- 【HTML/XML 10】XML文档中的Schema文件
导读:DTD是对XML文档进行有效性验证的方法之一,事实上,继DTD之后,出现了用来规范和描述XML文档的第二代标准:Schema.Schema是DTD的继承,但是也有其不同的地方,它是真正的以独立的 ...
- 在XML里的XSD和DTD以及standalone的使用3----具体使用详解
本人亲自写的一个简单的测试例子 1.xsd定义 <?xml version="1.0" encoding="utf-8"?><xs:schem ...
随机推荐
- excel 无法打开文件,提示:向程序发送命令时出现问题
以下的方法以Excel为例,请一个一个的使用,总会有一个适合你的. 1 .兼容性 鼠标右击桌面Excel(或其他)的快捷方式,选“兼容性”,把以管理员身份运行此程序前的勾去掉,就一切ok 了. 如果桌 ...
- 图标字体的使用(fontello.com)字体推荐及使用技巧
网页设计中为了页面漂亮好看,图标是少不了,网页中使用的图标通常都是使用图片,使用图片图标的有很多弊端,如果你经常制作网页应该有一肚子埋怨. 使用图片图标的弊端 放大图标必须重新作图, 改变颜色必须开启 ...
- python中os模块的常用接口和异常中Exception的运用
1.os.path.join(arg1, arg2) 将arg1和arg2对应的字符串连接起来并返回连接后的字符串,如果arg1.arg2为变量,就先将arg1.arg2转换为字符串后再进行连接. 2 ...
- iOS: 学习笔记实例, 用代码控制视图创建与切换
1. 创建iOS, Single View Application.2. 修改YYViewController.m // // YYViewController.m // DynamicViewDem ...
- 配置SHH集群
==特别要注意当前用户的问题== 1. 修改路由信息 vi /etc/hosts 10.211.55.15 hmaster1 10.211.55.16 hmaster2 10.211.55.17 hs ...
- webStorm中的混乱代码格式化
Mac上 command + alt + l windows上 control + alt + l
- property测试代码:
// // main.m // TestVar2 // // Created by lishujun on 14-9-4. // Copyright (c) 2014年 lishujun. All r ...
- 聊聊js运算符 ‘与(&&)’和‘ 或(||)’
一,先来几个问题,看给位能都全部答对. var objA1 = {x:1}; var objA2 = {x:2}; var resultA = objA1 && objA2; //请问 ...
- Ed Burns谈HTTP/2和Java EE Servlet 4规范
在2015年JavaLand大会上,Ed Burns展示了Java EE Servlet 4.0规范(JSR 369)的概要,演讲的重点在于Java EE平台对HTTP/2的支持.HTTP/2旨在解决 ...
- German Collegiate Programming Contest 2013:B
一个离散化的简单题: 我用的是STL来做的离散化: 好久没写离散化了,纪念一下! 代码: #include<cstdio> #include<cstring> #include ...