Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with validating sax parser

 
vijayraj kumar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic