• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with validating sax parser

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic