[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Java program for validating XML file with XSD - usingSun J


java code for validating xml
Hi,

try this for letting the parser working with W3C Schema :

factory.setAttribute(
             "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
             "http://www.w3.org/2001/XMLSchema");

Best regards,

A.Brillant
--------------------------------------------------
EditiX : XML Editor and XSLT Debugger
http://www.editix.com
--------------------------------------------------

Mukul Gandhi wrote:

>I have XML and XSD files as below:
>
>XML file
>--------
><?xml version="1.0" encoding="ISO-8859-1"?>
><shiporder orderid="889923"
>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
>                          
>xsi:noNamespaceSchemaLocation="shiporder.xsd">
> <orderperson>John Smith</orderperson>
> <shipto>
>  <name>Ola Nordmann</name>
>........ etc
>
>XSD file
>--------
><?xml version="1.0" encoding="ISO-8859-1" ?>
><xs:schema
>xmlns:xs="http://www.w3.org/2001/XMLSchema">
><xs:element name="shiporder">
> <xs:complexType>
>  <xs:sequence>
>   <xs:element name="orderperson" type="xs:string"/>
>   <xs:element name="shipto">
>    <xs:complexType>
>     <xs:sequence>
>........ etc
>
>Java program is
>---------------
>import javax.xml.parsers.DocumentBuilder;
>import javax.xml.parsers.DocumentBuilderFactory;
>import javax.xml.parsers.FactoryConfigurationError;
>import javax.xml.parsers.ParserConfigurationException;
>
>import org.xml.sax.SAXException;
>import org.xml.sax.SAXParseException;
>
>import java.io.File;
>import java.io.IOException;
>
>import org.w3c.dom.Document;
>import org.w3c.dom.DOMException;
>
>public class ValidateWithSchema {
>
>    static Document document;
>
>    public static void main(String argv[])
>    {
>        if (argv.length != 1) {
>            System.err.println("Usage: java
>ValidateWithSchema filename");
>            System.exit(1);
>        }
>
>        DocumentBuilderFactory factory =
>DocumentBuilderFactory.newInstance();
>        factory.setValidating(true);
>        factory.setNamespaceAware(true);
>        try {
>           DocumentBuilder builder =
>factory.newDocumentBuilder();
>           document = builder.parse(new
>File(argv[0]));
>        } catch (SAXException sxe) {
>
>           Exception  x = sxe;
>           if (sxe.getException() != null)
>               x = sxe.getException();
>           x.printStackTrace();
>
>        } catch (ParserConfigurationException pce) {
>
>            pce.printStackTrace();
>
>        } catch (IOException ioe) {
>
>           ioe.printStackTrace();
>        }
>    }
>
>}
>
>When I run the program as following -
>
>C:\xml>java ValidateWithSchema shiporder.xml
>
>Warning: validation was turned on but an
>org.xml.sax.ErrorHandler was not
>set, which is probably not what is desired.  Parser
>will use a default
>ErrorHandler to print the first 10 errors.  Please
>call
>the 'setErrorHandler' method to fix this.
>Error: URI=file:///C:/xml/shiporder.xml Line=2:
>Document is invalid: no grammar
>found.
>Error: URI=file:///C:/xml/shiporder.xml Line=2:
>Document root element "shiporder
>", must match DOCTYPE root "null".
>
>The error comes as shown above..
>
>Can someone please tell where is the problem?
>
>Regards,
>Mukul
>
>
>
>	
>		
>__________________________________ 
>Do you Yahoo!? 
>Yahoo! Mail - You care about security. So do we. 
>http://promotions.yahoo.com/new_mail
>
>-----------------------------------------------------------------
>The xml-dev list is sponsored by XML.org <http://www.xml.org>, an
>initiative of OASIS <http://www.oasis-open.org>
>
>The list archives are at http://lists.xml.org/archives/xml-dev/
>
>To subscribe or unsubscribe from this list use the subscription
>manager: <http://www.oasis-open.org/mlmanage/index.php>
>
>
>  
>



PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2011 All Rights Reserved.