久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用具有包含 XSD 的 Java 驗(yàn)證 XML 文件?

How to validate an XML file using Java with an XSD having an include?(如何使用具有包含 XSD 的 Java 驗(yàn)證 XML 文件?)
本文介紹了如何使用具有包含 XSD 的 Java 驗(yàn)證 XML 文件?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在使用 Java 5 javax.xml.validation.Validator 來驗(yàn)證 XML 文件.我已經(jīng)為一個(gè)只使用導(dǎo)入并且一切正常的模式完成了它.現(xiàn)在我正在嘗試使用另一個(gè)使用導(dǎo)入和一個(gè)包含的模式進(jìn)行驗(yàn)證.我遇到的問題是主模式中的元素被忽略,驗(yàn)證說它找不到他們的聲明.

I'm using Java 5 javax.xml.validation.Validator to validate XML file. I've done it for one schema that uses only imports and everything works fine. Now I'm trying to validate with another schema that uses import and one include. The problem I have is that element in the main schema are ignored, the validation says it cannot find their declaration.

這是我構(gòu)建架構(gòu)的方式:

Here is how I build the Schema:

InputStream includeInputStream = getClass().getClassLoader().getResource("include.xsd").openStream();
InputStream importInputStream = getClass().getClassLoader().getResource("import.xsd").openStream();
InputStream mainInputStream = getClass().getClassLoader().getResource("main.xsd").openStream();
Source[] sourceSchema = new SAXSource[]{includeInputStream , importInputStream, 
mainInputStream };
Schema schema = factory.newSchema(sourceSchema);

現(xiàn)在這里是 main.xsd 中聲明的摘錄

Now here is the extract of the declaration in main.xsd

<xsd:schema xmlns="http://schema.omg.org/spec/BPMN/2.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:import="http://www.foo.com/import" targetNamespace="http://main/namespace" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xsd:import namespace="http://www.foo.com/import" schemaLocation="import.xsd"/>
    <xsd:include schemaLocation="include.xsd"/>
    <xsd:element name="element" type="tElement"/>
    <...>
</xsd:schema>

如果我將包含的 XSD 的代碼復(fù)制到 main.xsd 中,它可以正常工作.如果我不這樣做,驗(yàn)證不會(huì)找到元素"的聲明.

If I copy the code of my included XSD in the main.xsd, it works fine. If I don't, validation doesn't find the declaration of "Element".

推薦答案

你需要使用一個(gè) LSResourceResolver 使其工作.請(qǐng)看下面的示例代碼.

you need to use an LSResourceResolver for this to work. please take a look at the sample code below.

驗(yàn)證方法:

// note that if your XML already declares the XSD to which it has to conform, then there's no need to declare the schemaName here
void 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));
}

資源解析器實(shí)現(xiàn):

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);
}

 }

資源解析器返回的輸入實(shí)現(xiàn):

The Input implemetation returned by the resource resolver:

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);
}
}

這篇關(guān)于如何使用具有包含 XSD 的 Java 驗(yàn)證 XML 文件?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 日本成人中文字幕在线观看 | 国产激情视频在线观看 | 国产精品久久久久久久久久久久久久 | 国产高清在线视频 | 狠狠干狠狠操 | 成人依人 | 久久99国产精一区二区三区 | 四虎成人在线播放 | 国产精品久久久久久久久久 | 日韩精品免费在线观看 | 在线成人免费av | 久久狼人天堂 | 福利视频亚洲 | 精品国产区 | 成人一区二区三区在线 | 亚洲 欧美 精品 | 成人永久免费视频 | 日韩午夜 | 久久精品一区二区 | 91亚洲国产成人久久精品网站 | 奇米av | 精品一区二区三区91 | 在线a视频 | 中文字幕一区二区三区乱码图片 | 成人午夜精品 | 国产精品国产a | 国产精品不卡 | 伊人狠狠操 | 视频一区二区中文字幕 | ririsao久久精品一区 | 91精品国产99久久 | 亚洲天堂久久 | 看一级毛片 | 成人精品国产免费网站 | 国产一区二区在线播放 | 色婷婷九月 | 国产精品片aa在线观看 | 日本精品一区二区三区视频 | av在线一区二区三区 | 毛片综合 | 希岛爱理在线 |