[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

No Subject

  • From: Richard Featherstone <R.Featherstone@u...>
  • To: xml-dev@l...
  • Date: Tue, 10 Jul 2001 14:01:42 +0100 (BST)

java xsd parser

Please help.  I'm trying to validate against a schema using Xerces and
JAXP.  I'm only using a simple schema to get things working, but they
don't.  Please look at the following code and let me know what I'm doing
wrong.

Thanks in advance

Richard


		HERE'S THE BASIC SCHEMA

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
elementFormDefault="qualified">
   <xsd:element name="message" type = "MessageType"/>
    
	<xsd:complexType name = "MessageType"> 
	   <xsd:choice>
		  <xsd:element name = "request" type = "xsd:string"/>
		  <xsd:element name="response" type = "xsd:string"/>
	   </xsd:choice>
	</xsd:complexType>
</xsd:schema>

		AND A VALID DOC

<?xml version="1.0" encoding="UTF-8"?>
<message xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\WINDOWS\Desktop\Inspiration\schema.xsd">
   <request>do something</request>
</message>

		AN INVALID DOC

<?xml version="1.0" encoding="UTF-8"?>
<message xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\WINDOWS\Desktop\Inspiration\schema.xsd">
   <badTag>should fail</badTag>
</message>

		AND THE JAVA CODE TO PARSE AND VALIDATE

import java.io.*;

import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;

public class Parse extends DefaultHandler {
    
    public void startElement(String uri, String localName, String
qualName, Attributes attrs) throws SAXException {
        System.out.println("Start\t" + localName);
    }
    
    public void characters (char buffer[], int offset, int length) throws
SAXException {
        if(length > 0){
            String temp = new String(buffer, offset, length);
            
            if(!temp.trim().equals("")){
                System.out.println("Content\t" + temp.trim());
            }
        }
    }
    
    public void error (SAXParseException spe) throws SAXParseException{
        throw spe;
    }
    
    public void warning (SAXParseException spe) throws SAXParseException{
        System.err.println("Warning: " + spe.getMessage());
    }
    
    public static void main (String args[]) {
            
        Parse p = new Parse();
        try{
            
            XMLReader parser =
(XMLReader)Class.forName("org.apache.xerces.parsers.SAXParser").newInstance();
            
            parser.setContentHandler(p);
            parser.setErrorHandler(p);
            parser.setFeature("http://xml.org/sax/features/validation",
true);
            parser.setFeature(
"http://apache.org/xml/features/validation/schema", true);
            //parser.setFeature(
"http://apache.org/xml/features/validation/schema-full-checking", true);
            
            parser.parse(new
InputSource("file:///C:/WINDOWS/Desktop/Inspiration/badDoc.xml"));
        }
        catch (SAXParseException spe){
            System.err.println("Parse Error: " + spe.getMessage());
        }
        catch(SAXException se){
            se.printStackTrace();
        }
        catch(Exception e){
            e.printStackTrace();
        }
        System.exit(0);
    }
}

		ERROR MESSAGE

Element type "message" must be declared


  • Follow-Ups:
    • RE:
      • From: Priscilla Walmsley <priscilla@w...>

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.