Validate xml using nested schemas

Discussion in 'Java' started by dhek, Aug 5, 2007.

  1. dhek

    dhek Guest

    Hi all,

    I would like to validate an xml file using nested schemas (that is schemas
    referring to other schemas).

    I've tried with the following setup, but it does not work an gives me an
    exception.

    public void validate(InputStream xmlDocument) {
    try {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);
    spf.setValidating(true);
    SAXParser sp = spf.newSAXParser();

    InputStream schemaA =
    this.getClass().getResourceAsStream("mySchema.xsd");
    InputStream schemaB =
    this.getClass().getResourceAsStream("mySchema1.xsd");

    InputStream[] schemas = {schemaA,schemaB};

    final String JAXP_SCHEMA_LANGUAGE =
    http://java.sun.com/xml/jaxp/properties/schemaLanguage;
    final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
    final String JAXP_SCHEMA_SOURCE =
    http://java.sun.com/xml/jaxp/properties/schemaSource;

    sp.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
    sp.setProperty(JAXP_SCHEMA_SOURCE, schemas);

    sp.parse(xmlDocument, new ParseErrorHandler());

    } catch (SAXException e) {
    System.out.println(e.toString());
    } catch (ParserConfigurationException e) {
    System.out.println(e.toString());
    } catch (IOException e) {
    System.out.println(e.toString());
    }
    }

    nb: mySchema.xsd contains a reference to mySchema1.xsd.

    So what I would like to know is what I'm doing wrong and which approach I
    should use instead.


    Hope someone can help.


    Best Regards,
    Daniel
    dhek, Aug 5, 2007
    #1
    1. Advertising

  2. dhek

    dhek Guest

    Maybe I should just pinpoint that I've imported my schemas into the
    namespace of the class that contains the method used (I've no trouble
    reading these or getting their paths, so seems my approach is wrong).

    Also the exception that I get seems to indicate that schema1.xsd is not
    found.


    Best Regards,
    Daniel


    "dhek" <> wrote in message
    news:46b58493$0$90270$...
    > Hi all,
    >
    > I would like to validate an xml file using nested schemas (that is schemas
    > referring to other schemas).
    >
    > I've tried with the following setup, but it does not work an gives me an
    > exception.
    >
    > public void validate(InputStream xmlDocument) {
    > try {
    > SAXParserFactory spf = SAXParserFactory.newInstance();
    > spf.setNamespaceAware(true);
    > spf.setValidating(true);
    > SAXParser sp = spf.newSAXParser();
    >
    > InputStream schemaA =
    > this.getClass().getResourceAsStream("mySchema.xsd");
    > InputStream schemaB =
    > this.getClass().getResourceAsStream("mySchema1.xsd");
    >
    > InputStream[] schemas = {schemaA,schemaB};
    >
    > final String JAXP_SCHEMA_LANGUAGE =
    > http://java.sun.com/xml/jaxp/properties/schemaLanguage;
    > final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
    > final String JAXP_SCHEMA_SOURCE =
    > http://java.sun.com/xml/jaxp/properties/schemaSource;
    >
    > sp.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
    > sp.setProperty(JAXP_SCHEMA_SOURCE, schemas);
    >
    > sp.parse(xmlDocument, new ParseErrorHandler());
    >
    > } catch (SAXException e) {
    > System.out.println(e.toString());
    > } catch (ParserConfigurationException e) {
    > System.out.println(e.toString());
    > } catch (IOException e) {
    > System.out.println(e.toString());
    > }
    > }
    >
    > nb: mySchema.xsd contains a reference to mySchema1.xsd.
    >
    > So what I would like to know is what I'm doing wrong and which approach I
    > should use instead.
    >
    >
    > Hope someone can help.
    >
    >
    > Best Regards,
    > Daniel
    >
    dhek, Aug 5, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Soeren
    Replies:
    2
    Views:
    344
    Waxolunist
    Sep 22, 2004
  2. Tony Prichard
    Replies:
    0
    Views:
    710
    Tony Prichard
    Dec 12, 2003
  3. Sean Bright
    Replies:
    2
    Views:
    430
    Sean Bright
    Dec 17, 2003
  4. Ultrus
    Replies:
    3
    Views:
    361
    Stefan Behnel
    Jul 9, 2007
  5. Andy B
    Replies:
    0
    Views:
    368
    Andy B
    Aug 7, 2008
Loading...

Share This Page