`
wenhai_zhang
  • 浏览: 186063 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

使用SAXParser(SAXParserFactory.newInstance())验证XML文件格式---XSD

    博客分类:
  • xml
阅读更多
SAXValidator.java----------------------------------------------------------
package com.xsd;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

class SAXValidator {
public static void main(String[] args) {
String schemaFeature = "http://apache.org/xml/features/validation/schema";
String fileName = "conf/xsd/dictionary_invalid_xsd.xml";
try {
File x = new File(fileName);
SAXParserFactory f = SAXParserFactory.newInstance();
System.out.println(f.toString());
f.setValidating(true);
f.setFeature(schemaFeature, true);
SAXParser p = f.newSAXParser();
System.out.println(p.toString());
DefaultHandler h = new MyErrorHandler();
p.parse(x, h);
} catch (ParserConfigurationException e) {
System.out.println(e.toString());
} catch (SAXException e) {
System.out.println(e.toString());
} catch (IOException e) {
System.out.println(e.toString());
}
}

private static class MyErrorHandler extends DefaultHandler {
public void warning(SAXParseException e) throws SAXException {
System.out.println("Warning: ");
printInfo(e);
}

public void error(SAXParseException e) throws SAXException {
System.out.println("Error: ");
printInfo(e);
}

public void fatalError(SAXParseException e) throws SAXException {
System.out.println("Fattal error: ");
printInfo(e);
}

private void printInfo(SAXParseException e) {
System.out.println("   Public ID: " + e.getPublicId());
System.out.println("   System ID: " + e.getSystemId());
System.out.println("   Line number: " + e.getLineNumber());
System.out.println("   Column number: " + e.getColumnNumber());
System.out.println("   Message: " + e.getMessage());
}
}
}

dictionary_invalid_xsd.xml----------------------------------------------
<?xml version="1.0"?>
<dictionary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="dictionary.xsd">
<!-- dictionary_invalid_xsd.xml
Copyright (c) 2002 by Dr. Herong Yang
-->
<word acronym="yes">
<name>XML</name>
<definition reference="Herong&apos;s Notes">
eXtensible Markup Language.
</definition>
<update date="23-Dec-2003" />
</word>
<word symbol="true">
<name><</name>
<definition>
Mathematical symbol representing the "less than" logical
operation, like: 1<2.
</definition>
<definition>
Reserved symbol in XML representing the beginning of tags,
like:
<![CDATA[<p>Hello world!</p>]]>
</definition>
<update editor="Herong Yang" />
</word>
<word symbol="no" acronym="false">
<name>extensible</name>
<definition>Capable of being extended.</definition>
</word>
</dictionary>

dictionary.xsd-------------------------------------------------------
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="dictionary" type="dictionaryType" />
<xsd:complexType name="dictionaryType">
<xsd:sequence>
<xsd:element name="word" type="wordType"
maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="wordType">
<xsd:sequence>
<xsd:element name="name" type="xsd:string" />
<xsd:element name="definition" type="definitionType"
maxOccurs="unbounded" />
<xsd:element name="update" type="updateType" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="acronym" type="xsd:boolean" use="optional" />
<xsd:attribute name="symbol" type="xsd:boolean" use="optional" />
</xsd:complexType>
<xsd:complexType name="definitionType" mixed="true">
<xsd:attribute name="reference" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="updateType">
<xsd:attribute name="date">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="\p{Nd}{4}-\p{Nd}{2}-\p{Nd}{2}" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:schema>
  • xsd.rar (2.4 KB)
  • 下载次数: 34
分享到:
评论

相关推荐

    java读取XML文件

    为了确保XML文件符合特定的架构(DTD或XSD),我们可以使用`javax.xml.validation`包中的`SchemaFactory`和`Validator`类。这样可以防止因错误的XML格式导致的问题。 最后,一些开发工具,如Eclipse、IntelliJ IDEA...

    Java通过XML Schema校验XML

    在本例中,作者选择使用DOM4j库来进行XML的解析与校验,并且结合了javax.xml.parsers包中的SAXParser来实现XML与XSD之间的校验。这种方法相对直观且易于理解。 #### 示例代码详解 首先,我们来看一下示例中给出的...

    xml读写

    SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { // 处理XML事件的方法 }; saxParser.parse...

    xml四种解析方法及源码

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File("file.xml")); // 使用doc对象...

    XML操作的例子

    在Java中,我们可以使用`org.xml.sax.helpers.DefaultHandler`和`javax.xml.parsers.SAXParserFactory`: ```java SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory....

    SAX

    - XML格式验证:验证XML文档是否符合特定的DTD或XSD规范。 - XML数据转换:将XML数据转换为其他格式,如JSON或数据库记录。 综上所述,SAX解析器是处理XML文档的一种高效策略,尤其适用于大文件或资源受限的环境...

    ReadXml.rar

    1. 验证:为了确保XML文件符合特定的结构规范,可以使用XML Schema(XSD)或DTD(Document Type Definition)进行验证。XML解析器在读取文件时可以检查这些验证规则。 2. 命名空间:XML命名空间用于解决元素和属性...

    android 网络编程详解代码第3章(网络下的XML解析和生成)

    SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); parser.parse(inputStream, new DefaultHandler() { @Override public void startElement(String ...

    关于XML的考试的试卷

    SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { @Override public void startElement(String uri...

    利用Java技术进行XML编程

    此外,`javax.xml.validation`包提供了XML Schema(XSD)验证功能,可以确保XML文档符合特定的结构规则。`org.w3c.dom.ls`和`javax.xml.parsers`包则包含了DOM Level 3的LS(Load and Save)功能,支持XML的读取和...

    JDOM SAX解析XML的对比及相关内容

    SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); parser.parse("example.xml", new SAXExample()); } } ``` 了解了JDOM和SAX的基本用法后,我们可以...

    XML解析

    SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); parser.parse("path_to_xml_file", new SAXExample()); } } ``` ### 工具支持 在实际开发中,许多...

Global site tag (gtag.js) - Google Analytics