What is the easiest way to obtain a statically typed representation of an XML schema (.XSD) in Java?
More specifically I want to be able to programatically traverse all defined simpleType:s and complexType:s in the XSD, obtain defined elements and their types, etc.
Something along the lines of:
for (XsdComplexType complexType : document.getDefinedComplexTypes()) {
..
}
Please note: I'm talking about an object representation of the .XSD document. I'm not looking for xjc style generation of Java classes from an XML schema.
One approach would be to simply apply standard XML reading tools to the .XSD file, but I'd assume there are open-source libraries around that could help me tackle the problem. As seen in the pseudo-code above I'd like a statically typed representation of the XSD document.
解决方案
Check out the Castor XML library's Schema class. And the SchemaReader to load it up. It should be exactly what you're looking for.
Contains methods like:
public java.util.Enumeration getComplexTypes()
Returns:
an Enumeration of all top-level ComplexType declarations