栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用Java和带有include的XSD验证XML文件?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用Java和带有include的XSD验证XML文件?

您需要使用LSResourceResolver才能正常工作。请查看下面的示例代码。

验证方法:

// note that if your XML already declares the XSD to which it has to conform, then there's no need to declare the schemaName herevoid validate(String xml, String schemaName) throws Exception {    documentBuilderFactory builderFactory = documentBuilderFactory.newInstance();    builderFactory.setNamespaceAware(true);    documentBuilder parser = builderFactory .newdocumentBuilder();    // parse the XML into a document object    document document = parser.parse(new StringInputStream(xml));    SchemaFactory factory = SchemaFactory .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);    // associate the schema factory with the resource resolver, which is responsible for resolving the imported XSD's    factory.setResourceResolver(new ResourceResolver()); // note that if your XML already declares the XSD to which it has to conform, then there's no need to create a validator from a Schema object    Source schemaFile = new StreamSource(getClass().getClassLoader() .getResourceAsStream(schemaName));    Schema schema = factory.newSchema(schemaFile);    Validator validator = schema.newValidator();    validator.validate(new DOMSource(document));}

资源解析器实现:

public class ResourceResolver  implements LSResourceResolver {public LSInput resolveResource(String type, String namespaceURI,        String publicId, String systemId, String baseURI) {     // note: in this sample, the XSD's are expected to be in the root of the classpath    InputStream resourceAsStream = this.getClass().getClassLoader() .getResourceAsStream(systemId);    return new Input(publicId, systemId, resourceAsStream);} }

资源解析器返回的Input实现:

public class Input implements LSInput {private String publicId;private String systemId;public String getPublicId() {    return publicId;}public void setPublicId(String publicId) {    this.publicId = publicId;}public String getbaseURI() {    return null;}public InputStream getByteStream() {    return null;}public boolean getCertifiedText() {    return false;}public Reader getCharacterStream() {    return null;}public String getEncoding() {    return null;}public String getStringData() {    synchronized (inputStream) {        try { byte[] input = new byte[inputStream.available()]; inputStream.read(input); String contents = new String(input); return contents;        } catch (IOException e) { e.printStackTrace(); System.out.println("Exception " + e); return null;        }    }}public void setbaseURI(String baseURI) {}public void setByteStream(InputStream byteStream) {}public void setCertifiedText(boolean certifiedText) {}public void setCharacterStream(Reader characterStream) {}public void setEncoding(String encoding) {}public void setStringData(String stringData) {}public String getSystemId() {    return systemId;}public void setSystemId(String systemId) {    this.systemId = systemId;}public BufferedInputStream getInputStream() {    return inputStream;}public void setInputStream(BufferedInputStream inputStream) {    this.inputStream = inputStream;}private BufferedInputStream inputStream;public Input(String publicId, String sysId, InputStream input) {    this.publicId = publicId;    this.systemId = sysId;    this.inputStream = new BufferedInputStream(input);}}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/414355.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号