【发布时间】:2021-06-24 15:12:06
【问题描述】:
我正在尝试验证 XML 是否符合 XSD。为此,我使用了xmlet/XsdParser,但除了 GitHub 页面之外,我找不到任何好的示例。
在自述文件中,他们只提供了如何将XSD 转换为相应的Java objects,但没有说明如何验证 XML 是否与 XSD 匹配。
以下是我拥有的读取 XSD 并创建 Java 对象的代码:
public class XSDParser {
public static void main(String[] args) throws URISyntaxException {
String xsdPath = Paths.get(XSDParser.class.getClassLoader().getResource("test.xsd").toURI()).toFile().getAbsolutePath();
String filePath = Path.of(xsdPath).toString();
XsdParser parserInstance = new XsdParser(filePath);
System.out.println(filePath);
Stream<XsdElement> elementsStream = parserInstance.getResultXsdElements();
Stream<XsdSchema> schemasStream = parserInstance.getResultXsdSchemas();
}
}
此代码读取 XSD 文件并创建元素的 Stream。现在我还添加了以下代码行,它们将读取 XML 文件并创建流:
String absolutePath = Paths.get(ApplicationMain.class.getClassLoader().getResource("inputXML.xml").toURI()).toFile()
.getAbsolutePath();
String path = Path.of(absolutePath).toString();
File xml = new File(path);
InputStream stream = new FileInputStream(xml);
现在,我想知道我的 XML 是否符合我已读取和存储的 XSD。如何做到这一点?
- 我想使用
XMLET/XSDPARSER来解析我的XSD 文件。 - 我正在使用
SAX PARSER解析我的 XML 文件。 - 我在网上找不到任何使用
XMLET/XSDPARSER实现的好的示例。
任何帮助将不胜感激。谢谢。
【问题讨论】:
标签: java xml xsd xml-parsing xsd-validation