Java program for validating XML file with XSD - using Sun JRE 1.4.2_03

From
Mukul Gandhi <>
To
Date
2005-07-14T08:53:37Z
ID
<>
Thread
Java program for validating XML file with XSD - using Sun JRE 1.4.2_03
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