简体   繁体   中英

Validate xml against xsd string

How can one validate an xml file against the multischema xsd which is in the form of a string? I have my XSD files retrieved from the database only at runtime and I don't want to create any files on the filesystem. So I have to validate XML against XSD strings .

Thanks

update: My actual problem is the import statements and include statements which are going to be file links . I want to know how to deal with import and include statements pointing to a string in the database. I mean I cannot at any point create a file. It is supposed to be a string.

Hi I made this small example.

I think it could be optimized but it gives you a global idea for a solution. :-)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM