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");
}

  

最新文章

  1. [OpenCV] Samples 04: contours2
  2. 如何用 fiddler 调试线上代码
  3. JVM最多能创建多少个线程:unabletocreatenewnativethread
  4. C#自学系列 - 开篇
  5. 更改EBS APPS 密码流程
  6. motan源码分析十:流量切换
  7. log4cpp日志不能是溶液子体积
  8. C# checked和unchecked详解
  9. IDL 数组相关函数
  10. K:正则表达式之进阶
  11. Unity的资源管理
  12. java垃圾回收机制GC
  13. 数据结构(C语言版)-C语言和C++相关补充
  14. [leetcode 120]triangle 空间O(n)算法
  15. EasyUI 删除
  16. 不开vip会员照样看vip电影(亲测有效)
  17. 执行次数最多的sql语句
  18. java 获取今天,昨天,上个月的日期
  19. 【解决】could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network
  20. React:快速上手(5)——掌握Redux(2)

热门文章

  1. python 之 面向对象(反射、__str__、__del__)
  2. PAT(B) 1030 完美数列 - C语言 - 滑动窗口 &amp; 双指针
  3. LOJ #3103. 「JSOI2019」节日庆典
  4. Drools 规则文件语法概述
  5. Ambari调整日志级别:How to enable debug logging in Ambari Server and Ambari Agent ?
  6. java之spring mvc之数据处理
  7. C# vb .net图像合成-合成文字
  8. Git for Windows. 国内镜像
  9. 2019-07-22 phpStudy配置虚拟主机
  10. 在DoNetMVC中使用控制反转和依赖注入【DI】