我正在生成一些xml文件,需要符合xsd文件给我。我该如何验证它们是否一致?
当前回答
如果你有一台Linux-Machine,你可以使用免费的命令行工具SAXCount。我发现这非常有用。
SAXCount -f -s -n my.xml
它针对dtd和xsd进行验证。 5s,一个50MB的文件。
在debian中,它位于包“libxerces-c-samples”中。
dtd和xsd的定义必须在xml中!你不能分别配置它们。
其他回答
使用Woodstox,配置StAX解析器以根据您的模式进行验证并解析XML。
如果异常被捕获,则XML无效,否则有效:
// create the XSD schema from your schema file
XMLValidationSchemaFactory schemaFactory = XMLValidationSchemaFactory.newInstance(XMLValidationSchema.SCHEMA_ID_W3C_SCHEMA);
XMLValidationSchema validationSchema = schemaFactory.createSchema(schemaInputStream);
// create the XML reader for your XML file
WstxInputFactory inputFactory = new WstxInputFactory();
XMLStreamReader2 xmlReader = (XMLStreamReader2) inputFactory.createXMLStreamReader(xmlInputStream);
try {
// configure the reader to validate against the schema
xmlReader.validateAgainst(validationSchema);
// parse the XML
while (xmlReader.hasNext()) {
xmlReader.next();
}
// no exceptions, the XML is valid
} catch (XMLStreamException e) {
// exceptions, the XML is not valid
} finally {
xmlReader.close();
}
注意:如果您需要验证多个文件,您应该尝试重用您的XMLInputFactory和XMLValidationSchema,以最大化性能。
对在线模式进行验证
Source xmlFile = new StreamSource(Thread.currentThread().getContextClassLoader().getResourceAsStream("your.xml"));
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(Thread.currentThread().getContextClassLoader().getResource("your.xsd"));
Validator validator = schema.newValidator();
validator.validate(xmlFile);
对本地模式进行验证
使用Java进行离线XML验证
使用Java 7,您可以遵循包描述中提供的文档。
// create a SchemaFactory capable of understanding WXS schemas SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // load a WXS schema, represented by a Schema instance Source schemaFile = new StreamSource(new File("mySchema.xsd")); Schema schema = factory.newSchema(schemaFile); // create a Validator instance, which can be used to validate an instance document Validator validator = schema.newValidator(); // validate the DOM tree try { validator.validate(new StreamSource(new File("instance.xml")); } catch (SAXException e) { // instance document is invalid! }
我只需要对XSD验证一次XML,所以我尝试了XMLFox。我发现这非常令人困惑和奇怪。帮助说明似乎与界面不匹配。
我最终使用了LiquidXML Studio 2008 (v6),它更容易使用,也更熟悉(UI与我经常使用的Visual Basic 2008 Express非常相似)。缺点:免费版没有验证功能,所以我不得不使用30天的试用期。
由于这是一个常见的问题,我将指出java也可以验证“引用”的xsd,例如,如果.xml文件本身在头文件中指定了xsd,则使用xsi:schemaLocation或xsi:noNamespaceSchemaLocation(或xsi用于特定的名称空间)ex:
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.example.com/document.xsd">
...
或schemaLocation(始终是命名空间到xsd映射的列表)
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.com/my_namespace http://www.example.com/document.xsd">
...
其他答案在这里也适用,因为.xsd文件“映射”到.xml文件中声明的名称空间,因为它们声明了一个名称空间,如果与.xml文件中的名称空间匹配,就没问题。但有时候有一个自定义解析器也很方便…
来自javadocs:“如果创建模式时没有指定URL、文件或源,那么Java语言将创建一个模式,它将在正在验证的文档中查找它应该使用的模式。例如:“
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = factory.newSchema();
这适用于多个名称空间等。 这种方法的问题是xmlsns:xsi可能是一个网络位置,因此在默认情况下,它会在每次验证时进入网络,而不是总是最优的。
下面是一个验证XML文件引用的任何XSD的例子(即使它必须从网络中获取它们):
public static void verifyValidatesInternalXsd(String filename) throws Exception {
InputStream xmlStream = new new FileInputStream(filename);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new RaiseOnErrorHandler());
builder.parse(new InputSource(xmlStream));
xmlStream.close();
}
public static class RaiseOnErrorHandler implements ErrorHandler {
public void warning(SAXParseException e) throws SAXException {
throw new RuntimeException(e);
}
public void error(SAXParseException e) throws SAXException {
throw new RuntimeException(e);
}
public void fatalError(SAXParseException e) throws SAXException {
throw new RuntimeException(e);
}
}
即使xml文件引用url,您也可以通过手动指定XSD(参见此处的其他答案)或使用“xml目录”样式的解析器来避免从网络中提取引用的XSD。Spring显然还可以拦截URL请求,为本地文件提供验证服务。或者你可以通过setResourceResolver设置自己的,例如:
Source xmlFile = new StreamSource(xmlFileLocation);
SchemaFactory schemaFactory = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema();
Validator validator = schema.newValidator();
validator.setResourceResolver(new LSResourceResolver() {
@Override
public LSInput resolveResource(String type, String namespaceURI,
String publicId, String systemId, String baseURI) {
InputSource is = new InputSource(
getClass().getResourceAsStream(
"some_local_file_in_the_jar.xsd"));
// or lookup by URI, etc...
return new Input(is); // for class Input see
// https://stackoverflow.com/a/2342859/32453
}
});
validator.validate(xmlFile);
请参见这里的另一个教程。
我相信默认是使用DOM解析,你可以用SAX解析器做一些类似的事情,也可以验证saxReader.setEntityResolver(your_resolver_here);
推荐文章
- 如何关闭mysql密码验证?
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder