Java - 使用 XSD 校验 XML

https://www.cnblogs.com/huey/p/4600817.html

这种方法不支持多个xsd文件,会报错

可以使用XMLBeans Tools来验证,3.1的版本用起来有问题,后来用2.6版本的就OK了

利用xmlbeans工具对xml格式进行验证(需要xsd文件)

https://blog.csdn.net/CronousGT/article/details/64137277

https://download.csdn.net/download/cronousgt/9787716#comment

http://xmlbeans.apache.org/docs/2.0.0/guide/tools.html

为了解决这个问题我们需要使用LSResourceResolver, SchemaFactory在解析shcema的时候可以使用LSResourceResolver加载外部资源。

XML validation for multiple schemas 验证使用多个XSD schema的XML文件

https://blog.csdn.net/hld_hepeng/article/details/6318663

Validating XML against XSD schemas in C#

https://samjenkins.com/validating-xml-against-xsd/

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Schema; namespace KetoLibrary.Xml
{
public class XsdValidator
{
public List<XmlSchema> Schemas { get; set; }
public List<String> Errors { get; set; }
public List<String> Warnings { get; set; } public XsdValidator()
{
Schemas = new List<XmlSchema>();
} /// <summary>
/// Add a schema to be used during the validation of the XML document
/// </summary>
/// <param name="schemaFileLocation">The file path for the XSD schema file to be added for validation</param>
/// <returns>True if the schema file was successfully loaded, else false (if false, view Errors/Warnings for reason why)</returns>
public bool AddSchema(string schemaFileLocation)
{
if (String.IsNullOrEmpty(schemaFileLocation)) return false;
if (!File.Exists(schemaFileLocation)) return false; // Reset the Error/Warning collections
Errors = new List<string>();
Warnings = new List<string>(); XmlSchema schema; using (var fs = File.OpenRead(schemaFileLocation))
{
schema = XmlSchema.Read(fs, ValidationEventHandler);
} var isValid = !Errors.Any() && !Warnings.Any(); if (isValid)
{
Schemas.Add(schema);
} return isValid;
} /// <summary>
/// Perform the XSD validation against the specified XML document
/// </summary>
/// <param name="xmlLocation">The full file path of the file to be validated</param>
/// <returns>True if the XML file conforms to the schemas, else false</returns>
public bool IsValid(string xmlLocation)
{
if (!File.Exists(xmlLocation))
{
throw new FileNotFoundException("The specified XML file does not exist", xmlLocation);
} using (var xmlStream = File.OpenRead(xmlLocation))
{
return IsValid(xmlStream);
}
} /// <summary>
/// Perform the XSD validation against the supplied XML stream
/// </summary>
/// <param name="xmlStream">The XML stream to be validated</param>
/// <returns>True is the XML stream conforms to the schemas, else false</returns>
private bool IsValid(Stream xmlStream)
{
// Reset the Error/Warning collections
Errors = new List<string>();
Warnings = new List<string>(); var settings = new XmlReaderSettings
{
ValidationType = ValidationType.Schema
};
settings.ValidationEventHandler += ValidationEventHandler; foreach (var xmlSchema in Schemas)
{
settings.Schemas.Add(xmlSchema);
} var xmlFile = XmlReader.Create(xmlStream, settings); try
{
while (xmlFile.Read()) { }
}
catch (XmlException xex)
{
Errors.Add(xex.Message);
} return !Errors.Any() && !Warnings.Any();
} private void ValidationEventHandler(object sender, ValidationEventArgs e)
{
switch (e.Severity)
{
case XmlSeverityType.Error:
Errors.Add(e.Message);
break;
case XmlSeverityType.Warning:
Warnings.Add(e.Message);
break;
}
}
}
}

  

public void MultipleSchemas()
{
var validator = new XsdValidator();
validator.AddSchema(@"SchemaDoc1.xsd");
validator.AddSchema(@"SchemaDoc2.xsd");
var isValid = validator.IsValid(@"ValidXmlDoc1.xml");
}

  

检验多个xsd的xml是否合法的更多相关文章

  1. 通过xsd schema结构来验证xml是否合法

    import sys import StringIO import lxml from lxml import etree from StringIO import StringIO # Constr ...

  2. 【转】XSD (xml Schema Definition)

    来自:http://www.cnblogs.com/newsouls/archive/2011/10/28/2227765.html Xml Schema的用途 1.  定义一个Xml文档中都有什么元 ...

  3. as3判断XML是否合法

    XML是否合法 在我认为 XML的标签成对 并且根标签外边没有其他东西 以下是合法的 <?xml version="1.0" encoding="utf-8&quo ...

  4. 根据框架的dtd或xsd生成xml文件

    下载Schema文件 首先要下载框架官网下的xsd或者xml文件,例如Spring官网地址,schema里面的就是xsd文件 Myeclipse中配置 我用的Myeclipse纯净版6.5,Windo ...

  5. xml to xsd ; xsd to xml

    xml to xsd 工具网站 https://www.freeformatter.com/xsd-generator.html 示例xml <?xml version="1.0&qu ...

  6. xsd与xml和类(class)对象之间的互相转换

    xsd与xml和类(class)对象之间的互相转换 . 第一:通过现有的已经写好的xsd来生成class(.cs)文件. 在您Visual Studio的安装目录下的SDKv2.0Bin中有个应用程序 ...

  7. C# xsd 验证 XML数据有效性 问题

    使用XSD进行批量数据导入时生成的XML数据有效性这样的功能已经不是第一次做了,之前做的时候都没有碰到什么问题,这些天在开发中遇到了一个很头痛的问题就是无论XSD文件规则怎么写,验证都是通过的. 下面 ...

  8. Java&Xml教程(九)Java中通过XSD校验XML合法性

    Java XML校验API能够通过XSD(XML Schema Definition)校验XML文件内容的合法性.在下面的案例中使用javax.xml.validation.Validator 类通过 ...

  9. Java&amp;Xml教程(九)Java中通过XSD校验XML合法性

    Java XML校验API可以通过XSD(XML Schema Definition)校验XML文件内容的合法性. 在以下的案例中使用javax.xml.validation.Validator 类通 ...

随机推荐

  1. C++ Primer第五版(中文带书签)

    本想发github的(链接更稳定),但是大小超出限制了. 本文件为扫描件,还是在我找了大半天之后的结果.能找到的免费的貌似都是扫描件,在看了一百多页之后(我不喜欢文字不能选中的感觉),我果断买了纸质书 ...

  2. 【数据结构】6.java源码ArrayList

    关于ArrayList的源码关注点 1.从底层数据结构,扩容策略2.ArrayList的增删改查3.特殊处理重点关注4.遍历的速度,随机访问和iterator访问效率对比 1.从底层数据结构,扩容策略 ...

  3. Navicat Premium 12 安装与破解,Navicat Premium通用的数据库管理工具

    本文转自:https://blog.csdn.net/WYpersist/article/details/86530973 Navicat Premium 是一套数据库开发工具,让你从单一应用程序中同 ...

  4. 用python编写一个搜索引擎

    完整代码如下: #!/usr/bin/env python #-*- coding: utf-8 -*- import sys import os import datetime from PyQt5 ...

  5. LeetCode runtime error

    今天在写LeetCode的某一道题目时候,遇到runtime error问题,本地能过,submit后死活不能通过. 查了一下网上的一些答案,基本上都是数组.指针没有初始化造成野指针.数组索引值越界. ...

  6. Python入门 .变量 常量 基础数据类型 用户输入 流程控制语句 小练习题

    # 2.name = input(“>>>”)通过代码来验证name变量是什么数据类型?--str name = input(">>>") pr ...

  7. CentOS7安装MySQL(完整版)

    在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB. 1 下载并安装MySQL官方的 Yum Re ...

  8. word表格中怎么添加递增的序号

    word2013表格中怎么添加递增的序号?word2013表格中想要让第一类自动显示递增序号,该怎么操作呢?下面我们就来分享两种方法,需要的朋友可以参考下 工具/原料   word2013 通过项目编 ...

  9. httpd服务的配置及应用

    一.httpd服务的配置文件 httpd服务的主配置文件通常为httpd根目录下的conf/httpd.conf文件,通过yum安装的httpd服务的主配置路径通常如下: httpd-2.2:/etc ...

  10. Linux 软链接和硬链接简介

    在Linux系统中,将文件分为两个部分:用户数据和元数据. 元数据(inode) 元数据即文件的索引节点(inode),用来记录文件的权限(r.w.x).文件的所有者和属组.文件的大小.文件的状态改变 ...