// parse an XML document into a DOM tree
DocumentBuilder parser = DocumentBuilderFactory.newInstance//.newDocumentBuilder//;
Document document = parser.parse/new File/"instance.xml"//;
// 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 DOMSource/document//;
} catch /SAXException e/ {
// instance document is invalid!
}
private void validateXmlAgainstSchema/final MyValidationEventCollector vec, final String xmlFileName, final String xsdSchemaName, final Class rootClass/ {
try /InputStream xmlFileIs = Thread.currentThread//.getContextClassLoader//.getResourceAsStream/xmlFileName/;/ {
final JAXBContext jContext = JAXBContext.newInstance/rootClass/;
// Unmarshal the data from InputStream
final Unmarshaller unmarshaller = jContext.createUnmarshaller//;
final SchemaFactory sf = SchemaFactory.newInstance/XMLConstants.W3C_XML_SCHEMA_NS_URI/;
final InputStream schemaAsStream = Thread.currentThread//.getContextClassLoader//.getResourceAsStream/xsdSchemaName/;
unmarshaller.setSchema/sf.newSchema/new StreamSource/schemaAsStream///;
unmarshaller.setEventHandler/vec/;
unmarshaller.unmarshal/new StreamSource/xmlFileIs/, rootClass/.getValue//; // The Document class is the root object in the XML file you want to validate
for /String validationError : vec.getValidationErrors/// {
logger.trace/validationError/;
}
} catch /final Exception e/ {
logger.error/"The validation of the XML file " + xmlFileName + " failed: ", e/;
}
}
class MyValidationEventCollector implements ValidationEventHandler {
private final List<string> validationErrors;
public MyValidationEventCollector// {
validationErrors = new ArrayList<>//;
}
public List<string> getValidationErrors// {
return Collections.unmodifiableList/validationErrors/;
}
@Override
public boolean handleEvent/final ValidationEvent event/ {
String pattern = "line {0}, column {1}, error message {2}";
String errorMessage = MessageFormat.format/pattern, event.getLocator//.getLineNumber//, event.getLocator//.getColumnNumber//,
event.getMessage///;
if /event.getSeverity// == ValidationEvent.FATAL_ERROR/ {
validationErrors.add/errorMessage/;
}
return true; // you collect the validation errors in a List and handle them later
}
}
// 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//;
}
13 个回复
卫东
赞同来自:
http://java.sun.com/j2se/1.5.0 ... .html
.
恒工厂方案是一个字符串
, 这决定了 XSDs. 上面的代码检查描述符 WAR deployment 向 URL
, 但您也可以轻松检查它与本地文件相关联。
你不应该使用 DOMParser 验证文档 /如果只有您的目标不是在任何情况下创建文档对象模型/. 这将导致对象的创造 DOM 随着该文件的分析 - 如果你不打算使用它们,这是浪费的。
喜特乐
赞同来自:
http://xerces.apache.org/xerces2-j/
. 这个教程
http://www.ibm.com/developerwo ... .html
/req. signup/.
原始归属:从这里肆无忌惮地复制 :
风见雨下
赞同来自:
现在顽皮的配置文件将无法满足我们的装配!
http://ant.apache.org/manual/T ... .html
窦买办
赞同来自:
或者
/或者 xsi 对于某些名称空间/
http://www.ibm.com/developerwo ... .html
:
或者 SchemaLocation /始终列出显示空间地图 xsd/
其他答案也在这里工作,因为 .xsd 文件 "map" 到文件中声明的名称的名称空间 .xml, 因为它们声明名称空间,并且如果与文件中的命名空间一致 .xml, 你很好。 但有时它有很方便
...
的 javadocs: "如果您在不指定的情况下创建方案 URL, 文件或来源,然后是语言 Java 创建在已验证文档中查找文档的文件,以查找它应该使用的图表。 例如:"
它适用于几个名称空间等。
这种方法的问题是
, 它可能是一个网络位置,所以默认情况下,它会随着每个支票出去并落入网络中并不总是最佳的。
以下是一个检查文件的示例 XML 关于任何参考文献 it XSD /即使他必须将它们拉出网络/:
你可以避免拉动参考 XSD 来自网络,即使文件 xml 参考 url, 指定 xsd 手动 /在这里看到其他一些答案。/ 或使用
https://coderoad.ru/25698764/
"XML catalog" . 显然 Spring 还
询问 URL 维护本地文件进行检查。 或者你可以通过自己安装
https://docs.oracle.com/javase ... lver/
, ex:
也可以看看
https://docs.oracle.com/javase ... .html
另一个教程。
我相信默认情况下使用解析 DOM, 你可以像解析器那样做点什么 SAX, 哪一个
https://blog.frankel.ch/use-lo ... -xml/
检查
</document></document>
詹大官人
赞同来自:
http://docs.oracle.com/javase/ ... .html
.
冰洋
赞同来自:
创建
/写/, 您可以在录制期间检查内容,而不是先写入,然后读取检查。 你可能会这样做 JDK API 检查 Xml, 如果您正在使用 SAX-based writer: 如果是这样,只需通过调用连接验证器 'Validator.validate/source, result/', 来源来自你的地方 writer, 结果是结论应该去的地方。
此外,如果你使用 Stax 用于录制内容 /或使用或可以使用的库 stax/, Woodstox
https://github.com/FasterXML/woodstox
使用时可以直接维护支票 XMLStreamWriter. 这里
http://www.cowtowncoder.com/bl ... .html
, 显示它是如何完成的:
风见雨下
赞同来自:
他检查 dtd 和 xsd.
5s 对于文件大小 50 MB。
在 debian squeeze 它在包装中 "libxerces-c-samples".
定义 dtd 和 xsd 必须是B. xml! 您无法单独配置它们。
郭文康
赞同来自:
http://xmlbeans.apache.org/
. 使用命令行工具 XMLBeans 自动创建并打包一组对象 Java 基于 XSD. 然后可以使用这些对象来构建文档。 XML 基于此计划。
它有内置支持检查方案并可转换 Java 文档中的对象 XML 反之亦然。
http://www.castor.org/
和
http://java.sun.com/developer/ ... jaxb/
- 这些是其他图书馆 Java, 谁提供与之相同的目标 XMLBeans.
董宝中
赞同来自:
</string></string>
石油百科
赞同来自:
至于图书馆,那么事实上就是标准 Xerces2
http://xerces.apache.org
这是一个版本
http://xerces.apache.org/xerces-c/
, 所以我。
http://xerces.apache.org/xerces2-j/
.
然而,请记住,这是一个沉重的解决方案。 但再次,检查 XML vs. XSD 文件是一个相当严重的问题。
至于该工具为您做到这一点,
http://www.xmlfox.com/xml_editor.htm
它似乎有价值的决定,但没有个人使用它,我不能肯定地说。
快网
赞同来自:
本地计划
帅驴
赞同来自:
https://github.com/FasterXML/woodstox
, 调整解析器 StAX 检查您的计划和解析 XML.
如果捕获了例外,那么 XML 它是不允许的,否则有效:
笔记
: 如果需要检查多个文件,则必须尝试重用您的
和
, 最大化性能。
知食
赞同来自:
我完成了我用的东西 LiquidXML Studio 2008 /v6/, 哪个更容易使用,更多的立即签名 /UI 非常相似 Visual Basic 2008 Express, 我经常使用/. 缺点:在免费版本中缺席检查的能力,因此我必须使用30天的试用版。