Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #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



  #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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.