Click here to Skip to main content
12,887,625 members (37,192 online)
Rate this:
Please Sign up or sign in to vote.
See more: XML Java

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 javax.xml.XMLConstants;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
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(
		// SchemaFactory is a schema compiler. It reads external representations
		// of schemas and prepares them for validation.
		SchemaFactory schemaFactory = SchemaFactory
		// This object represents a set of constraints that can be checked/
		// enforced against an XML document.
		Schema schema = schemaFactory.newSchema(new File(
		// A processor that checks an XML document against Schema.
		Validator validator = schema.newValidator();
		try {
			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

1 solution

Rate this: bad
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!

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170424.1 | Last Updated 25 Jul 2012
Copyright © CodeProject, 1999-2017
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