我正在生成一些xml文件,需要符合xsd文件给我。我该如何验证它们是否一致?
当前回答
Java运行时库支持验证。上次我检查的是Apache Xerces解析器。您可能应该使用javax.xml.validation.Validator。
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;
import java.net.URL;
import org.xml.sax.SAXException;
//import java.io.File; // if you use File
import java.io.IOException;
...
URL schemaFile = new URL("http://host:port/filename.xsd");
// webapp example xsd:
// URL schemaFile = new URL("http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd");
// local file example:
// File schemaFile = new File("/location/to/localfile.xsd"); // etc.
Source xmlFile = new StreamSource(new File("web.xml"));
SchemaFactory schemaFactory = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
Schema schema = schemaFactory.newSchema(schemaFile);
Validator validator = schema.newValidator();
validator.validate(xmlFile);
System.out.println(xmlFile.getSystemId() + " is valid");
} catch (SAXException e) {
System.out.println(xmlFile.getSystemId() + " is NOT valid reason:" + e);
} catch (IOException e) {}
模式工厂常量是字符串http://www.w3.org/2001/XMLSchema,它定义了xsd。上面的代码针对URL http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd验证WAR部署描述符,但您也可以很容易地针对本地文件进行验证。
您不应该使用DOMParser来验证文档(除非您的目标是创建文档对象模型)。这将在解析文档时开始创建DOM对象——如果你不打算使用它们,这是一种浪费。
其他回答
如果你有一台Linux-Machine,你可以使用免费的命令行工具SAXCount。我发现这非常有用。
SAXCount -f -s -n my.xml
它针对dtd和xsd进行验证。 5s,一个50MB的文件。
在debian中,它位于包“libxerces-c-samples”中。
dtd和xsd的定义必须在xml中!你不能分别配置它们。
Java运行时库支持验证。上次我检查的是Apache Xerces解析器。您可能应该使用javax.xml.validation.Validator。
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;
import java.net.URL;
import org.xml.sax.SAXException;
//import java.io.File; // if you use File
import java.io.IOException;
...
URL schemaFile = new URL("http://host:port/filename.xsd");
// webapp example xsd:
// URL schemaFile = new URL("http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd");
// local file example:
// File schemaFile = new File("/location/to/localfile.xsd"); // etc.
Source xmlFile = new StreamSource(new File("web.xml"));
SchemaFactory schemaFactory = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
Schema schema = schemaFactory.newSchema(schemaFile);
Validator validator = schema.newValidator();
validator.validate(xmlFile);
System.out.println(xmlFile.getSystemId() + " is valid");
} catch (SAXException e) {
System.out.println(xmlFile.getSystemId() + " is NOT valid reason:" + e);
} catch (IOException e) {}
模式工厂常量是字符串http://www.w3.org/2001/XMLSchema,它定义了xsd。上面的代码针对URL http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd验证WAR部署描述符,但您也可以很容易地针对本地文件进行验证。
您不应该使用DOMParser来验证文档(除非您的目标是创建文档对象模型)。这将在解析文档时开始创建DOM对象——如果你不打算使用它们,这是一种浪费。
对在线模式进行验证
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验证
使用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
推荐文章
- 如何关闭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