我想使用 Xerces 从字符串加载 XML 模式,但直到现在,我只能从 URI 加载它:
final XMLSchemaLoader xsLoader = new XMLSchemaLoader();
final XSModel xsModel = xsLoader.loadURI(file.toURI().toString());
可用的加载方法:
XSLoader {
public XSModel load(LSInput is) { }
public XSModel loadInputList(LSInputList is) { }
public XSModel loadURI(String uri) { }
public XSModel loadURIList(StringList uriList) { }
}
是否有从字符串加载 XML 模式的选项?在我的上下文中,处理是在客户端完成的,因此不能使用 URI 方法。
谢谢。
最佳答案
我不是特别熟悉你的问题,但我从 ProgramCreek 中找到了这个有用的代码片段它演示了如何从 LSInput
对象(上面列出的第一个方法)中获取 XSModel
。还可以从输入流加载 XML 模式。我稍微修改了代码以达到以下目的:
private LSInput getLSInput(InputStream is) throws InstantiationException,
IllegalAccessException, ClassNotFoundException {
final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
final DOMImplementationLS impl = (DOMImplementationLS)registry.getDOMImplementation("LS");
LSInput domInput = impl.createLSInput();
domInput.setByteStream(is);
return domInput;
}
用法:
// obtain your file through some means
File file;
LSInput ls = null;
try {
InputStream is = new FileInputStream(file);
// obtain an LSInput object
LSInput ls = getLSInput(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
if (ls != null) {
XMLSchemaLoader xsLoader = new XMLSchemaLoader();
XSModel xsModel = xsLoader.load(ls);
// now use your XSModel object here ...
}
关于java - Xerces - 从字符串加载模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35870964/