Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XML
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 4th, 2004, 12:50 PM
Registered User
 
Join Date: Jul 2004
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem with validating sax parser

Hi all,

I am trying to validating xml against xml schema 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:positiveInteger"/>
    <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:positiveInteger"/>
    <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



Reply With Quote
  #2 (permalink)  
Old April 18th, 2006, 10:15 AM
Registered User
 
Join Date: Apr 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi
Were you able to figure out the problem. I am facing the same problem!

Thanks,
Satish

Quote:
quote:Originally posted by vijay3000
 Hi all,

I am trying to validating xml against xml schema 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:positiveInteger"/>
    <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:positiveInteger"/>
    <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



Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to use sax parser pavanntcs XSLT 3 September 13th, 2006 02:02 AM
How to parse xml using SAX Parser?? priyatowin XSLT 2 September 8th, 2006 02:56 AM
SAX Parser Error dpan168 XML 0 October 26th, 2004 04:53 PM
XML Parser Problem?? mar0364 XML 1 September 15th, 2004 09:23 AM
Parser Problem mar0364 XML 1 July 9th, 2003 06:50 AM



All times are GMT -4. The time now is 06:01 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.