1

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.

Vladimir Vukanac
  • 944
  • 16
  • 29
user1900588
  • 181
  • 1
  • 3
  • 15
  • Whichever you think can do this. Java,python...any language. – user1900588 Aug 01 '13 at 04:35
  • Most languages can do it. The answer will depend on which languages you use – John Saunders Aug 01 '13 at 14:29
  • 1
    @JohnSaunders I couldn't find anything on this. Plenty of programs can be found on using string xmls as input, but nothing on import and include statements. Can you point out any example or something on this. Again, I want to know how to resolve import and include statements as strings. Most example only deal with xml string inputs. – user1900588 Aug 05 '13 at 05:46

1 Answers1

0

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();
        }
    }
}
sgirardin
  • 428
  • 5
  • 12
  • For the transformation from String to InputStream I used this post: http://stackoverflow.com/questions/247161/how-do-i-turn-a-string-into-a-stream-in-java – sgirardin Jul 31 '13 at 14:27
  • 1
    Thanks for your response. Please check my update on post. I can read the files as strings easily but what I need is the import statements and include statements. How to map import and include to strings rather than file links. – user1900588 Aug 01 '13 at 04:33