Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Validate xml using nested schemas

Thread view: 
dhek - 05 Aug 2007 09:04 GMT
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 - 05 Aug 2007 09:09 GMT
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

> Hi all,
>
[quoted text clipped - 47 lines]
> Best Regards,
> Daniel


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.