我正在生成一些xml文件,需要符合xsd文件给我。我该如何验证它们是否一致?
当前回答
下面是如何使用Xerces2来实现这一点。这方面的教程在这里(req。注册)。
原始出处:公然抄袭:
import org.apache.xerces.parsers.DOMParser;
import java.io.File;
import org.w3c.dom.Document;
public class SchemaTest {
public static void main (String args[]) {
File docFile = new File("memory.xml");
try {
DOMParser parser = new DOMParser();
parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setProperty(
"http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
"memory.xsd");
ErrorChecker errors = new ErrorChecker();
parser.setErrorHandler(errors);
parser.parse("memory.xml");
} catch (Exception e) {
System.out.print("Problem parsing the file.");
}
}
}
其他回答
使用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! }
如果你有一台Linux-Machine,你可以使用免费的命令行工具SAXCount。我发现这非常有用。
SAXCount -f -s -n my.xml
它针对dtd和xsd进行验证。 5s,一个50MB的文件。
在debian中,它位于包“libxerces-c-samples”中。
dtd和xsd的定义必须在xml中!你不能分别配置它们。
由于这是一个常见的问题,我将指出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);
使用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,以最大化性能。
我们使用ant构建我们的项目,所以我们可以使用schemvalidate任务来检查我们的配置文件:
<schemavalidate>
<fileset dir="${configdir}" includes="**/*.xml" />
</schemavalidate>
现在淘气的配置文件将失败我们的构建!
http://ant.apache.org/manual/Tasks/schemavalidate.html
推荐文章
- 在maven中安装mvn到底做什么
- 不可变与不可修改的集合
- 如何在JSON中使用杰克逊更改字段名
- GSON -日期格式
- 如何从线程捕获异常
- 无法解析主机"<URL here>"没有与主机名关联的地址
- 如何在Java中打印二叉树图?
- String.format()在Java中格式化双重格式
- com.jcraft.jsch.JSchException: UnknownHostKey
- XSD和WSDL之间的区别是什么?
- Java中的操作符重载
- 如何加速gwt编译器?
- 在Hibernate中重新连接分离对象的正确方法是什么?
- 应该……接住环内还是环外?
- 如何格式化Joda-Time DateTime仅为mm/dd/yyyy?