检验多个xsd的xml是否合法
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是否合法的更多相关文章
- 通过xsd schema结构来验证xml是否合法
import sys import StringIO import lxml from lxml import etree from StringIO import StringIO # Constr ...
- 【转】XSD (xml Schema Definition)
来自:http://www.cnblogs.com/newsouls/archive/2011/10/28/2227765.html Xml Schema的用途 1. 定义一个Xml文档中都有什么元 ...
- as3判断XML是否合法
XML是否合法 在我认为 XML的标签成对 并且根标签外边没有其他东西 以下是合法的 <?xml version="1.0" encoding="utf-8&quo ...
- 根据框架的dtd或xsd生成xml文件
下载Schema文件 首先要下载框架官网下的xsd或者xml文件,例如Spring官网地址,schema里面的就是xsd文件 Myeclipse中配置 我用的Myeclipse纯净版6.5,Windo ...
- xml to xsd ; xsd to xml
xml to xsd 工具网站 https://www.freeformatter.com/xsd-generator.html 示例xml <?xml version="1.0&qu ...
- xsd与xml和类(class)对象之间的互相转换
xsd与xml和类(class)对象之间的互相转换 . 第一:通过现有的已经写好的xsd来生成class(.cs)文件. 在您Visual Studio的安装目录下的SDKv2.0Bin中有个应用程序 ...
- C# xsd 验证 XML数据有效性 问题
使用XSD进行批量数据导入时生成的XML数据有效性这样的功能已经不是第一次做了,之前做的时候都没有碰到什么问题,这些天在开发中遇到了一个很头痛的问题就是无论XSD文件规则怎么写,验证都是通过的. 下面 ...
- Java&Xml教程(九)Java中通过XSD校验XML合法性
Java XML校验API能够通过XSD(XML Schema Definition)校验XML文件内容的合法性.在下面的案例中使用javax.xml.validation.Validator 类通过 ...
- Java&Xml教程(九)Java中通过XSD校验XML合法性
Java XML校验API可以通过XSD(XML Schema Definition)校验XML文件内容的合法性. 在以下的案例中使用javax.xml.validation.Validator 类通 ...
随机推荐
- 个人GitHub资源分享仓库
个人GitHub资源分享仓库 门牌号:https://github.com/ZeroPhong/Learning-Resource 2019年10月27日 注册GitHub账号,仓库建立: 当天上 ...
- 关于elasticsearch使用G1垃圾回收替换CMS
最近ES集群数据节点经常出现jvm占用过高,频繁GC导致ES集群卡死,很长时间才恢复.在网上看到用G1垃圾回收可以改善这一情况,但都是老版本的ES,我们现在使用的版本是5.5.2,所以想问问各位5.5 ...
- PHP的序列化、对象、反射、异常与错误
1. 怎么理解php里面的序列化与反序列化? 序列化是将对象转换为字节流.反序列化就是将流转换为对象. 这两个过程结合起来,可以轻松地存储和传输数据,在网络中可以做到跨平台.快速传输. 两种序列化方式 ...
- asp.net core 之中间件
Http请求资源的过程可以看成一个管道:“Pipe”,并不是所有的请求都是合法的.安全的,其于功能.性能或安全方面的考虑,通常需要在这管道中装配一些处理程序来筛选和加工这些请求.这些处理程序就是中间件 ...
- 3.matplotlib绘制条形图
plt.bar() # coding=utf-8 from matplotlib import pyplot as plt from matplotlib import font_manager my ...
- Vue父组件向子组件传值以及data和props的区别
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/xukongjing1/article/ ...
- 【转载】C#中List集合使用LastIndexOf判断元素最后一次出现的索引位置
在C#的List集合操作中,有时候需要判断元素对象在List集合中第一次出现的索引位置信息,此时需要使用到List集合的IndexOf方法来判断,如果元素存在List集合中,则IndexOf方法返回所 ...
- kindedtor 数据传输问题
<script src="/static/kindeditor/kindeditor-all.js"></script><script src=&qu ...
- githe和github连接,上传
Git入门 如果你完全没有接触过Git,你现在只需要理解通过Git的语法(敲入一些命令)就可以将代码上传到远程的仓库或者下载到本地的仓库(服务器),可知我们此时应该有两个仓库,就是两个放代码的地方,一 ...
- Order by 默认排序方式
--ORDER BY 默认排序方式为升序ASC:SELECT * FROM [TABLE_NAME] ORDER BY [COLUMN_NAME] ESC;--升序DESC:SELECT * FROM ...