java-Xerces-从字符串加载架构
作者:互联网
我想使用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 ...
}
标签:xsd,schema,xerces,java 来源: https://codeday.me/bug/20191027/1943072.html