XSD Validation

Hello,

   I tried to validate my XML file with XSD in a Jav Program, using xerces parser.

 Here is my files.

XML file(order.xml)
----------

<?xml version="1.0" encoding="UTF-8"?>
<shipOrder
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="order_schema.xsd"
>
  <shipTo>
    <name>Tove Svendson</name>
    <street>Ragnhildvei 2</street>
    <address>4000 Stavanger</address>
    <country>Norway</country>
  </shipTo>
  <items>
    <item>
      <title>Empire Burlesque</title>
      <quantity>1</quantity>
      <price>10.90</price>
    </item>
    
  </items>
</shipOrder>


XSD File(order_schema.xsd)
-----------

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="shipOrder" type="order"/> 
 <xsd:complexType name="shipAddress">
 <xsd:sequence>
 <xsd:element   name="name"      type="xsd:string"/>
 <xsd:element   name="street"    type="xsd:string"/>
 <xsd:element   name="address"   type="xsd:string"/>
 <xsd:element   name="country"   type="xsd:string"/>
 </xsd:sequence>
 </xsd:complexType>

 <xsd:complexType name="cdItem">
 <xsd:sequence>
 <xsd:element   name="title"     type="xsd:string"/>
 <xsd:element   name="quantity"  type="xsd:positiveInteger"/>
 <xsd:element   name="price"     type="xsd:decimal"/>
</xsd:sequence>
 </xsd:complexType>

 <xsd:complexType name="cdItems">
 <xsd:sequence>
 <xsd:element   name="item"      type="cdItem"/>
 </xsd:sequence>
 </xsd:complexType>

 <xsd:complexType name="order">
 <xsd:sequence>
 <xsd:element   name="shipTo"    type="shipAddress"/>
 <xsd:element   name="items"     type="cdItems"/>
</xsd:sequence>
 </xsd:complexType>

 </xsd:schema>

My Java files are
-----------------

1)MyErrorHandler.java

import org.xml.sax.*;
import org.xml.*;
class MyErrorHandler implements ErrorHandler 
{
	public void warning (SAXParseException e) throws SAXException 
	{
		System.err.println("Warning");
		System.err.println(e);
		throw e;	
	}
	public void error (SAXParseException e) throws SAXException 
	{
		System.err.println("Error");
		Sy

	public void fatalError (SAXParseException e) throws SAXException 
	{
		System.err.println("Fatal Error");
		System.err.println(e);
		throw e;
	}


}


2) DOMValidator.java
----------------------
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

public class DOMValidator 
{
	public static void main(String args[])
	{
		if (args.length != 1)
		{
			System.out.println("Usage :java DOMValidator yourfile.xml");
			System.exit(1);
		}
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		factory.setValidating(false);
		System.out.println(args[0] + "is valid");
		
		try
		{
		DocumentBuilder builder = factory.newDocumentBuilder();
		builder.setErrorHandler(new MyErrorHandler());
		Document doc = builder.parse(new File(args[0]) );
		}
		catch (Exception e)
		{
			System.err.println(e);
		}
	}
	
}


 In the Java first we have to compile MyErrorHandler.java file and the compile DOMValidator.javafile.

 When im trying to validate the order.xml by giving the command

  "java DOMValidator order.xml"

Its throwing errors like

Error
org.xml.sax.SAXParseException: Element type "shipOrder must be declared".

  Im not getting the whats this error, i think my XML and XSD files are correct.

 Can anyone help me out for solving this problem.

Thanks in advance

Bye
Vinod
 

Received on Monday, 4 February 2002 05:03:26 UTC