Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: XML Java
Hi,
 
I wrote java code for xml validation against xsd file.In that if any error in xml it throws the exception.Instead of that i want that program to return result in string like what is the problem.
 
package com.xmlpack;
import java.io.File;
import java.io.IOException;
import javax.xml.XMLConstants;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;
import org.xml.sax.SAXException;
public class SampleXmlValidator  {
	public static void main(String args[]) throws SAXException, IOException {
		// Construct a StreamSource from a File.
		Source xmlFile = new StreamSource(new File(
				"D:/SampleXML/sample/src/com/xml/product.xml"));
		// SchemaFactory is a schema compiler. It reads external representations
		// of schemas and prepares them for validation.
		SchemaFactory schemaFactory = SchemaFactory
				.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		// This object represents a set of constraints that can be checked/
		// enforced against an XML document.
		Schema schema = schemaFactory.newSchema(new File(
				"D:/SampleXML/sample/src/com/xsd/product.xsd"));
		// A processor that checks an XML document against Schema.
		Validator validator = schema.newValidator();
	
		try {
			validator.validate(xmlFile);
			System.out.println(xmlFile.getSystemId() + " is valid");
		} catch (SAXException e) {
			System.out.println(xmlFile.getSystemId() + " is NOT valid");
			System.out.println("Reason: " + e.getLocalizedMessage());
		}
	}
}
 

in google search i found that we can use validator.validate(Source,Result) method.
I want to know how to implement the method..
Posted 24-Jul-12 22:45pm
ammu23372

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

we can use validator.validate(Source,Result) method. I want to know how to implement the method
How about reading about it: Class Validator[^]
 
Try out!
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 402
1 Nirav Prabtani 258
2 Richard Deeming 250
3 _Amy 195
4 CHill60 175
0 Nirav Prabtani 265
1 OriginalGriff 177
2 _Amy 170
3 Mehdi Gholam 105
4 Maciej Los 90


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 25 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100