我正在研究使用JDom解析XML文檔的應用程序。如何根據使用JDom的XSD模式驗證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;
}
現在我也想驗證XML針對XSD。我讀了API,它告訴使用JAXP和東西,我不知道如何。
該應用程序使用JDom 1.1.1和我在網上找到的例子使用了一些在這個版本中不可用的類。有人可以解釋如何針對XSD驗證XML,特別是針對此版本。
JDOM2是否支持XSD 1.1? (特別是'xs:assert')?我搜索了一下,但仍然找不到答案 – ycomp
@ycomp - JDOM不執行實際的解析,它使用底層解析器(通常是xerces或與Java打包的xerces版本)來執行此操作。 JDOM只是做內存模型。我相信Xerces支持XSD1.1,請參閱:http://xerces.apache.org/xerces2-j/xml-schema.html#supported-schema-1.1-features – rolfl