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

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

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

[ Lists Home | Date Index | Thread Index ]
  • To: xml-dev@lists.xml.org
  • Subject: Java program for validating XML file with XSD - using Sun JRE 1.4.2_03
  • From: Mukul Gandhi <mukul_gandhi@yahoo.com>
  • Date: Thu, 14 Jul 2005 01:53:37 -0700 (PDT)
  • Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:Received:Date:From:Subject:To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=PsFU6SIKJiif4MogXQyDMSakPC3p/K5C4LZlDZWQ0b1Ag5OKz7ND/KuXeG0UaetneW6LL0b8lzhQ/zm1VPJQKiEFA1S/E6nTqx6xNol1dJRQwzDNd3d1AtHd3f/JQGYfFt2Ab4s4mKyx4XMRP0nVZFJRDXobcJwuzzumR+V9azQ= ;

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




 

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

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