• 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

Validating xml against xsd using dom

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Validating xml against xsd code:

private static final String schemaSource = "schema.xsd";
private static final String JAXP_SCHEMA_SOURCE ="http://java.sun.com/xml/jaxp/properties/schemaSource";


DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
documentBuilderFactory.setValidating(validation);
try {
documentBuilderFactory.setAttribute(JAXP_SCHEMA_SOURCE,new File(schemaSource));
}
catch (IllegalArgumentException x) {
x.printStackTrace();
}
documentBuilder.setErrorHandler(new IMSParserErrorHandler());
Document document = documentBuilder.parse(configFile);

my schema is in : c:\data\schema.xsd..

i have the c:\data in my classpath.. so i gave the relative path as schema.xsd to my schema url.

error is :

Message: schema_reference.4: Failed to read schema document 'IMSSchema.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.


XML

<sample xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='schema.xsd'>

</sample>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

</xsd:schema>
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Priyadharshini Nagarajan:
Validating xml against xsd code:

my schema is in : c:\data\schema.xsd..

i have the c:\data in my classpath.. so i gave the relative path as schema.xsd to my schema url.

error is :

Message: schema_reference.4: Failed to read schema document 'IMSSchema.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.


XML

<sample xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='schema.xsd'>

</sample>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

</xsd:schema>



For validation purpose, the classpath doesn't play a role, as I understand. Since you declared a relative path for the schema location, place the XSD file in the same directory as the XML file and try validating.

- m
 
Priyadharshini Nagarajan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi


<sample xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='schema.xsd'>

</sample>


Tell me what the above code will do. this is not validating my xml.i also made setValidating to true.

is validation can be only in code or xml also.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Priyadharshini Nagarajan:
Hi


<sample xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='schema.xsd'>

</sample>


Tell me what the above code will do. this is not validating my xml.i also made setValidating to true.

is validation can be only in code or xml also.




Validation is only in code, not in XML file. An XML file can reference an XSD (or DTD) for validation. When you parse such an XML file using a validating parser, the parser can then validate and tell you if the XML file is valid according to the XSD that is referenced in the file.

ummmm....lets continue this discussion in your other thread since you already posted code in that thread. I am closing this thread as they are duplicated.
Thanks.

- m
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic