This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes XSD validation in XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XSD validation in XML" Watch "XSD validation in XML" New topic
Author

XSD validation in XML

Ani kattoor
Greenhorn

Joined: Jun 12, 2011
Posts: 2
Hi,

Suppose if I have an XSD and corresponding XML. And I need to validate this XSD against this XML.

I could able to do it using the below code,

SchemaFactory factory =
SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
File schemaLocation = new File("samp.xsd");
Schema schema = factory.newSchema(schemaLocation);
Validator validator = schema.newValidator();
String file= "File.xml";
Source source = new StreamSource(file));
try {
validator.validate(source);
}
catch (SAXParseException ex) {
System.out.println(file +"is not valid because ");
System.out.println(ex.getMessage());

}
}

But if the xml is invalid, I want to get the exact message on which tag having the error. But using the above code I am not able to display that message.

I am getting the error as

cvc-datatype-valid.1.2.1: 'Ang' is not a valid value for 'integer'.

But I want to get the information like on which tag its showing error.

can anybody help me on this?

Regards,
Aneesh
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12760
    
    5
Carefully examine the JavaDocs for org.xml.sax.SAXParseException.

You will discover the methods: getLineNumber and getColumnNumber

The line and column should be close to the offending tag.

Bill


Ani kattoor
Greenhorn

Joined: Jun 12, 2011
Posts: 2
Thanks a lot for your valuable reply William.

But with the line number and column number its very difficult to identify the tag if it is a big XML file.


Is there any other option for this to get the tag name??

Regards,
Ani
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
i dont think so you can get the tag name using SAXParseException object... if its a big xml how does a tag name would help rather than line / col numbers since there will be repetitions of hundreds of tags with the same name!

what i have with my Errorhanlder -

private void printInfo(SAXParseException e) {
System.out.println(" Public ID: "+e.getPublicId());
System.out.println(" System ID: "+e.getSystemId());
System.out.println(" Line number: "+e.getLineNumber());
System.out.println(" Column number: "+e.getColumnNumber());
System.out.println(" Message: "+e.getMessage());
}

Please post the solution if you got one!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12760
    
    5
But with the line number and column number its very difficult to identify the tag if it is a big XML file.


Why is it difficult?

If the file is too large to bring into a text editor, write a short program to read the file line by line and display the XML text in the vicinity of the designated line number.

Personally, I use the UltraEdit-32 programmer's editor for problems like this because it can display the hex value of characters.

Bill
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Moving to our XML forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XSD validation in XML
 
Similar Threads
valiating xml using multiple schemas
validating xml
Unable to validate XML against the schema
pom.xml is not valid because cvc-elt.1: Cannot find the declaration of element 'project'.
Error Validating huge XSD with XML in a Servlet