gpt4 book ai didi

java - 如何使用 JDom 根据 XSD 架构验证 XML 文档

转载 作者:行者123 更新时间:2023-12-01 16:34:43 25 4
gpt4 key购买 nike

我正在开发一个使用 JDom 解析 XML 文档的应用程序。

以下是现有代码:

private Document openDocumentAtPath(File file) {

// Create a sax builder for building the JDOM document
SAXBuilder builder = new SAXBuilder();

// JDOM document to be created from XML document
Document doc = null;

// Try to build the document
try {

// Get the file into a single string
BufferedReader input = new BufferedReader(
new FileReader( file ) );
String content = "";
String line = null;
while( ( line = input.readLine() ) != null ) {
content += "\n" + line;
}

StringReader reader = new StringReader( content );
doc = builder.build(reader);


}// Only thrown when a XML document is not well-formed
catch ( JDOMException e ) {
System.out.println(this.file + " is not well-formed!");
System.out.println("Error Message: " + e.getMessage());
}
catch (IOException e) {
System.out.println("Cannot access: " + this.file.toString());
System.out.println("Error Message: " + e.getMessage());
}
return doc;
}

现在我还想根据 XSD 验证 XML。我读了API它告诉我要使用 JAXP 之类的东西,但我不知道如何操作。

该应用程序使用JDom 1.1.1,我在网上找到的示例使用了一些在此版本中不可用的类。有人可以解释如何根据 XSD 验证 XML,特别是对于此版本。

最佳答案

简单地复制粘贴 JDOM FAQ 中的代码怎么样? ?

关于java - 如何使用 JDom 根据 XSD 架构验证 XML 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10524104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com