aspose file tools*
The moose likes XML and Related Technologies and the fly likes problem with validating sax parser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "problem with validating sax parser" Watch "problem with validating sax parser" New topic
Author

problem with validating sax parser

vijayraj kumar
Greenhorn

Joined: Jul 04, 2004
Posts: 1
Hi all,

I am trying to validating xml against xml schama using validating

sax parser with jaxp.

my code compling and running sucessfully but it not validating

against xml schema

what was the problem with this code?

Sax.java
----------
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.ErrorHandler;
import javax.xml.parsers.SAXParser;
public class SAX extends DefaultHandler {

static final String JAXP_SCHEMA_LANGUAGE =


"http://java.sun.com/xml/jaxp/properties/schemaLanguage";

static final String W3C_XML_SCHEMA =
"http://www.w3.org/2001/XMLSchema";

static final String JAXP_SCHEMA_SOURCE =


"http://java.sun.com/xml/jaxp/properties/schemaSource";


public static void main(String[] args)
{
try{
DefaultHandler handler=new SAX();
SAXParserFactory

factory=SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
SAXParser saxparser=factory.newSAXParser();
saxparser.setProperty(JAXP_SCHEMA_LANGUAGE,

W3C_XML_SCHEMA);
System.out.println("hello");


//saxparser.setProperty(JAXP_SCHEMA_SOURCE,"myemploy.xsd");
saxparser.parse("myemploy.xml",handler);

}
catch(SAXParseException se)
{


System.out.println(se);
}
catch(Exception t)
{


System.out.println("After getting error..");
t.printStackTrace();
}

}
}

myemploy.xsd
------------
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="address" type="xsd:string"/>
<xsd:element name="age" type="xsd ositiveInteger"/>
<xsd:element name="employ">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="eno"/>
<xsd:element ref="ename"/>
<xsd:element ref="age"/>
<xsd:element ref="esal"/>
<xsd:element ref="address"/>
<xsd:element ref="phno"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="employees">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" minOccurs="1"

ref="employ"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="ename" type="xsd:string"/>
<xsd:element name="eno" type="xsd ositiveInteger"/>
<xsd:element name="esal" type="xsd:string"/>
<xsd:element name="phno" type="xsd:string"/>
</xsd:schema>


myemploly.xml
-----------
<?xml version="1.0"?>
<employees>
<employ>
<eno>123</eno>
<ename>kishore</ename>
<age>-12</age>
<esal>2000</esal>
<address>maverickdr</address>
<phno>123</phno>
</employ>
</employees>

Plese help me.

Thanks in advance
vijay
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
My guess would be that you have commented out this line:

If you don't tell the parser which schema it should validate against, how would it know to do that?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
jQuery in Action, 2nd edition
 
subject: problem with validating sax parser