 |
 |
 |
You may find what you are looking for in an open-source package hosted by
Eclipse:
http://www.eclipse.org/xsd
Good Luck,
Bob
Scalable XML Infrastructure
IBM Thomas J Watson Research Center
Yorktown Heights, New York, USA
Lachlan Aldred
<l.aldred@q...
.au> To
Sent by: xmlschema-dev@w...
xmlschema-dev-req cc
uest@w...
Subject
Schema API
06/20/2004 10:46
PM
Does anyone know a good XML Schema API?
I had a quick look at W3 XML Schema API Web pages, also XERCES/PSVI to see
if it could offer the sort of features I was after, and I have some
questions:
How would I load a just a schema (no instance) to create some schema
objects in Java?
How do I serialize parts of the schema (for example one type def) to create
a self contained schema declaration? This is problematic when a type
definition refers to another root level type/element definition elsewhere
in the document, or in another document.
If I have a schema object how can I validate instances against it?
Out of interest I developed a program in Java that uses Xerces to validate
a schema against the schema specification and it gives line number reports
etc. It's a bit of a hack. It takes schemas as strings, which suits my
purposes, and if they are valid returns an empty string. Is there a
cleaner way of doing that too?
import org.xml.sax.XMLReader;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.helpers.DefaultHandler;
import org.jdom.input.SAXBuilder;
import java.io.StringReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;
public class SchemaValidator extends DefaultHandler {
StringBuffer _errorsString = new StringBuffer("");
private static SchemaValidator _myInstance;
private SAXBuilder _builder;
private File _tempSchema;
/**
* If the string is of 0 length then you know it has passed the test.
* @return the validation failures as a string.
*/
public String validateSchema(String schema) {
File userDir = new File(System.getProperty("user.dir"));
_tempSchema = new File(userDir, "_tempSchema.xsd");
try {
FileWriter writer = new FileWriter(_tempSchema);
writer.write(schema);
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
String instance =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<document xmlns:xsi=\"
http://www.w3.org/2001/XMLSchema-instance\">" +
"</document>";
String result = checkSchema(new InputSource(new
StringReader(instance)));
_tempSchema.delete();
return result;
}
public static SchemaValidator getInstance() {
if (_myInstance == null) {
_myInstance = new SchemaValidator();
}
return _myInstance;
}
private SchemaValidator() {
_builder = new SAXBuilder();
_builder.setValidation(false);
}
public void warning(SAXParseException ex) {
addMessage(ex, "Warning");
}
public void error(SAXParseException ex) {
addMessage(ex, "Invalid");
}
public void fatalError(SAXParseException ex) throws SAXException {
addMessage(ex, "Error");
}
private void addMessage(SAXParseException e, String errType) {
String lineNum = getLineNumber(e);
if (lineNum != null) {
_errorsString.append(errType + "#" + lineNum + "# " +
e.getMessage() + '\n');
}
}
private String getLineNumber(SAXParseException e) {
String fileURL = e.getSystemId();
if (fileURL != null) {
return
"[ln: " + e.getLineNumber() + " col: " +
e.getColumnNumber() + "]";
}
return null;
}
private String checkSchema(InputSource input) {
_errorsString.delete(0, _errorsString.length());
try {
XMLReader parser = setUpChecker();
parser.parse(input);
} catch (SAXParseException e) {
} catch (Exception e) {
e.printStackTrace();
}
return _errorsString.toString();
}
private XMLReader setUpChecker() throws SAXException {
XMLReader parser =
XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
parser.setContentHandler(this);
parser.setErrorHandler(this);
parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setFeature("http://apache.org/xml/features/validation/schema
", true);
parser.setFeature("
http://apache.org/xml/features/validation/schema-full-checking", true);
try {
URL schemaURL = _tempSchema.toURL();
parser.setProperty("
http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation
",
"" + schemaURL);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return parser;
}
}
Regards,
Lachlan
Lachlan Aldred
Queensland University of Technology
Australia
From nobody@w... Mon Jun 21 20:12:50 2004
Return-Path: <nobody@w...>
X-Original-To: xmlschema-dev+aa@l...
Delivered-To: xmlschema-dev+aa@l...
Received: from wiggum.w3.org (wiggum.w3.org [18.29.0.99])
|
 | 



|  |
These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.
|  |
| |
 |
 |
 |