OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   Re: [xml-dev] Associating an XML Schema with a XML Document

[ Lists Home | Date Index | Thread Index ]
  • To: "'List - XML-Dev'" <xml-dev@lists.xml.org>
  • Subject: Re: [xml-dev] Associating an XML Schema with a XML Document
  • From: Mike Rawlins <mcr@rawlinsecconsulting.com>
  • Date: Sat, 01 Nov 2003 17:44:21 -0600
  • In-reply-to: <00a201c3a021$d428bb70$6501a8c0@lotus.com>
  • References: <000901c39ea2$e9dd9790$6901a8c0@bedford.progress.com>

Luciano,

You'll need to set several options in your DocumentBuilderFactory class 
before you create your document.  Setting these will tell the parser to 
validate against the referenced schema when it loads and parses the 
instance document.

Here's how I typically do it, with comments:

         //  Set up DOM XML environment
         DocumentBuilderFactory Factory =
           DocumentBuilderFactory.newInstance();

         //  Set the factory to create a Document Builder that
         //    is:
         //  Namespace aware - necessary for schema validation
         Factory.setNamespaceAware(true);
         //  Ignores whitespace on Element only nodes
         Factory.setIgnoringElementContentWhitespace(true);
         //  Ignores comments
         Factory.setIgnoringComments(true);
         //  Set the schema language - these attributes are
         //  specific to Xerces2
         Factory.setAttribute(JAXPConstants.JAXP_SCHEMA_LANGUAGE,
           JAXPConstants.W3C_XML_SCHEMA);
         //  Validating, if requested
         if (boValidate)
         {
           Factory.setValidating(true);
         }

         //  Create the new document builder
         DocumentBuilder Builder = Factory.newDocumentBuilder();

The JAXP constants are set by:

import org.apache.xerces.jaxp.JAXPConstants;

(assuming, of course, that you're using Xerces).

Hope this helps,

Mike

At 09:42 PM 10/31/2003 -0500, Luciano Resende (Discussion List) wrote:
>Thank you all for all the help, looks like i got the xml working, at least 
>my xml editor recognize it as a valid xml :
>
>
><?xml version="1.0" encoding="UTF-8"?>
>
><books 
>xsi:noNamespaceSchemaLocation="file:///d:/ibm/dev/workspaces/dominoportal/dominoadapter/booksextended.xsd"
>
>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
>
>.....
>
>
>
>I have a sample JAXP/DOM code working with the validation based on a DTD, 
>can somebody give any sample on how to use a parser to validate the xml 
>document using the XML Schema associated with it ?
>
>

---------------------------------------------------------------
Michael C. Rawlins, Rawlins EC Consulting
www.rawlinsecconsulting.com
Using XML with Legacy Business Applications (Addison-Wesley, 2003)
www.awprofessional.com/titles/0321154940





 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS