java - Xerces - 从字符串加载模式

标签 java xsd schema 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 ...
}

关于java - Xerces - 从字符串加载模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35870964/

相关文章:

java - 有没有办法判断 JFrame 是否为 "Maximised"(MS Windows)

java - 查找字符串中的小数

xml - 如何使 maven-jaxb2-plugin 忽略生成类(如果它已存在于我的项目源中)

mysql - 如何将表中的字段粘贴到具有相同属性的另一个表中

hadoop - Hbase 模式是否已读取?

apache-spark - 修改 Spark 数据框中的结构列

java - Cassandra read_request_timeout_in_ms 为外部(客户端)请求设置

java - 如何使用 jaxb-xjc 模式在生成的类中添加额外的方法

java - 如何在 XML 中指定复杂类型的列表?

java react 堆filterAndMap?