根据xsd字符串验证xml



如何根据字符串形式的多模式xsd验证xml文件?我只在运行时从数据库中检索XSD文件,并且我不想在文件系统上创建任何文件。因此,我必须根据XSD字符串验证XML。

感谢

更新:我的实际问题是导入语句和include语句,它们将成为文件链接。我想知道如何处理指向数据库中字符串的导入和包含语句。我的意思是,我在任何时候都无法创建文件。它应该是一根绳子。

嗨,我举了这个小例子。

我认为它可以进行优化,但它为您提供了一个全球性的解决方案。:-)

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.SchemaFactory;
import org.xml.sax.SAXException;
public class xsd {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String xsd = "Your XSD";
        String xsd2 = "2nd XSD";
        InputStream is = new ByteArrayInputStream(xsd.getBytes());
        InputStream is2 = new ByteArrayInputStream(xsd2.getBytes());
        SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Source source = new StreamSource(is);
        Source source2 = new StreamSource(is2);
        try {
            sf.newSchema(new Source[]{source,source2});
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }
}

最新更新