• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XSD validation in XML

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to our XML forum.
reply
    Bookmark Topic Watch Topic
  • New Topic