wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes Validating xml against  xsd using dom Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Validating xml against  xsd using dom" Watch "Validating xml against  xsd using dom" New topic
Author

Validating xml against xsd using dom

Priyadharshini Nagarajan
Greenhorn

Joined: Sep 24, 2004
Posts: 22
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>
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
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


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Priyadharshini Nagarajan
Greenhorn

Joined: Sep 24, 2004
Posts: 22
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

Joined: Jun 03, 2000
Posts: 5040
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.
 
subject: Validating xml against xsd using dom
 
Similar Threads
Parse/Validate with DSIG schema issue
Error in parsing XML
problem with validating sax parser
Schema Validation
Issue in validating vanila XML