W3C home > Mailing lists > Public > www-xml-schema-comments@w3.org > July to September 2002

Validating xml using a schema with domparser does not work

From: by way of <Jimmy.Hombrouckx@electrabel.com>
Date: Tue, 20 Aug 2002 08:04:35 -0600
Message-Id: <5.1.0.14.1.20020820080431.06b54c30@localhost>
To: W3C XML Schema Comments list <www-xml-schema-comments@w3.org>





Hi,

I encountered problems, when trying to validate xml with a schema. I used
several methods as you can see in the code below.
If anyone can see what's wrong with it, please tell!!!

Thx,

Jimmy


The code (that won't work!):

String xmlReceived="...";
DOMParser parser = new DOMParser();
StringReader reader = new StringReader(xmlReceived);
InputSource inputSource = new InputSource(reader);

try{
	/*
	// I tried adding the schema location in the xml stream itself but
never got an exception when using a bad xml
	// Like this:
	<"xsi:noNamespaceSchemaLocation='C:/.../testSchema.xsd'>" +
xmlReceived+
	// And also like this:			
	"<xsi:schemaLocation=\"C:/.../testSchema.xsd\">" + xmlReceived;

	// Also like this:
	xmlReceived = ""+
	"<document"+
	  "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"+
	  "xsi:noNamespaceSchemaLocation='C:/.../testSchema.xsd'>"+
	"</document>"+
	xmlReceived;
	// Even like this:
	"<?xml version=\"1.0\"?>"+
	"<xml_root xmlns:xsi=\" http://www.w3.org/2001/XMLSchema-instance"+
	"<http://www.w3.org/2001/XMLSchema-instance> \""+
	"xsi:noNamespaceSchemaLocation=\"C:/.../testSchema.xsd\">"+
	xmlReceived;
	*/

	// No I'm trying with the properties
	// This properties seem to work	
	parser.setFeature("http://xml.org/sax/features/validation", true);
	// But schema validation doesn't
	
parser.setFeature("http://apache.org/xml/features/validation/schema",true);
	
	// Also tested these features
	
//parser.setFeature("http://apache.org/xml/features/validation/schema-full-c
hecking",true);
	
//parser.setFeature("http://apache.org/xml/features/validation/dynamic",true
);
	
	// Property doesn't work at all, doesn't even look for the file!
	
parser.setProperty("http://apache.org/xml/properties/schema/external-schemaL
ocation", "C:/.../testSchema.xsd");
	
//parser.setProperty("http://apache.org/xml/properties/schema/external-schem
aLocation", "Choose whatever you want!");	
	// This property checks if the file exists, but it doesn't throw an
exception	when using a bad xml
	
parser.setProperty("http://apache.org/xml/properties/schema/external-noNames
paceSchemaLocation", "C:/.../testSchema.xsd");

	parser.parse(inputSource);
}catch(Exception e){
	e.printStackTrace();
	throw new NoSuchElementException("Parsing failed");
} 
Received on Tuesday, 20 August 2002 10:07:31 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Sunday, 6 December 2009 18:13:01 GMT