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. Java的访问修饰符的作用范围

    访问修饰符: private default protected public 作用范围: 访问修饰符\作用范围 所在类 同一包内其他类 其他包内子类 其他包内非子类 private 可以访问 不可以 ...

  2. AVR单片机教程——EasyElectronics Library v1.2手册

    索引: bit.h delay.h pin.h wave.h pwm.h led.h rgbw.h button.h switch.h segment.h 主要更新: 添加了segment.h的文档: ...

  3. Selenium+Java(七)Selenium对话框的处理

    HTML代码如图所示: 一.alert String url = "file:///C:/Users/ex_yuhao/Desktop/index.html"; //引用IE浏览器 ...

  4. WPF 判断一个对象是否是设计时的窗口类型,而不是运行时的窗口

    原文:WPF 判断一个对象是否是设计时的窗口类型,而不是运行时的窗口 当我们对 Window 类型写一个附加属性的时候,在属性变更通知中我们需要判断依赖对象是否是一个窗口.但是,如果直接判断是否是 W ...

  5. Linux RedHat 7 配置本地 YUM源

    尽管RPM安装方法能够帮助用户查询软件相关的依赖关系,但是还是需要安装人员自己来解决,而且有些大型软件可能与数十个程序都有依赖关系,在这种情况下安装软件事件非常痛苦和耗费事件的事情,而Yum软件仓库可 ...

  6. c# 结构体实现数据新增(数据字段较多的情况使用) 一

    点击新增按钮 { ChkFormIDBox.Text = Coeno.DevChk.DevChk.CleanUpInput(ChkFormIDBox.Text);  --清除表单文本框数据输入 if ...

  7. 自己用JQueryUI封装了几个系统常用对话框

    /* * @功能描述:各种系统消息框 * @前置插件:JQueryUI * @开 发 者:魏巍 * @开发日期:2015-04-15 * @version 1.0 */ var SF = {}; SF ...

  8. SparkStreaming+kafka Receiver模式

    1.图解 2.过程 1.使用Kafka的High Level Consumer API 实现,消费者不能自己去维护消费者offset,而且kafka也不关心数据是否丢失. 2.当向zookeeper中 ...

  9. windows下控制台程序实现窗口显示

    windows下实现窗口显示,如果限定是C/C++语言,并且是原生Windows支持,需要使用GDI或GDI+.一般是在Visual Studio里新建Win32应用程序,而不是Win32 conso ...

  10. nodejs 删除空文件

    var fs = require("fs") var path = require("path") var listRealPath = path.resolv ...