Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Data Validation help

    Hello everyone,

    Can anyone please suggest me with some examples of how to validate data on the server side. I tried out the Javascript and looked at some regex methods. The regex methods actually use methods from the class Pattern. I cannot find examples that use the Validator class. I was told to read the Javadocs but I am not able to understand how those methods work.

    So I would appreciate if anyone could provide me with links to examples for validation at the server site.

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Data Validation help

    Javascript would be something you'd use on the client side.

    The Validator class is for validating XML against a Schema. Here's an excerpt from a program I'm working on right now. In this snippet, doc is a Document that was passed into the method. XMLInput is the name of the class in which this code appears.

    Java Code:
    StreamSource source =
    	new StreamSource(XMLInput.class.getResourceAsStream("/schema/Input.xsd"));
    SchemaFactory sf =
    	SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    Schema schema = sf.newSchema(source);
    Validator validator = schema.newValidator();
    // make it throw exceptions for all errors
    validator.setErrorHandler(new ErrorHandler() {
    	@Override
    	public void error(SAXParseException e) throws SAXException {
    		throw e;				
    	}
    	@Override
    	public void fatalError(SAXParseException e) throws SAXException {
    		throw e;					
    	}
    	@Override
    	public void warning(SAXParseException e) throws SAXException {
    		throw e;					
    	}
    });
    validator.validate(new DOMSource(doc));
    That last line would throw a SAXException if the Document wasn't complete and correct according to the Schema.
    Last edited by kjkrum; 01-28-2012 at 05:30 PM.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. data type validation
    By cagipple in forum New To Java
    Replies: 2
    Last Post: 12-14-2011, 08:57 AM
  2. XML Validation
    By sehudson in forum XML
    Replies: 5
    Last Post: 03-21-2011, 01:38 PM
  3. Replies: 4
    Last Post: 01-30-2011, 12:18 AM
  4. Replies: 0
    Last Post: 09-01-2008, 12:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •