views:

264

answers:

5

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.

A: 

Why not just read it in as a DOM Document and then do whatever you want to do with it?

Thorbjørn Ravn Andersen
From the question: "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."
knorv
Reconsider stating what you need to actually DO (as in terms of problem to solve). What I see from your edited statement is easily doable in XSLT with XPath.
Thorbjørn Ravn Andersen
A: 

While I agree with and want to second the other two (at the moment) answers that proposed using a standard tool such as dom4j or jdom, I'd like to posit an off-the-wall suggestion. You can use JiBX or another XML data binding tool to produce Java objects of your liking directly from the schema XML.

Or you could just use a standard DOM parser.

Max A.
Please see my updated post with pseudo-code. Hopefully that clears things up.
knorv
Max: I think you mean apply JAXB (or other) to the "Schema for Schemas" to generate java objects that correspond to XML Schema elements. This schema for schema does not completely specify XML Schema, but it may be enough for binding. It's in appendix A of the XML Schema specification: http://www.w3.org/TR/2004/REC-xmlschema-1-20041028/#normative-schemaSchema
13ren
@13ren: In my travels I've found JAXB to be a bit over-engineered. But that's just my opinion. JiBX can seem that way too, when applied incorrectly. At any rate, I feel that there's little difference between using an OO Schema representation and a standard DOM. To justify the former, the POJOs need to carry some intrinsic value, such as Schema-specific accessors that would be non-trivial to implement in plain DOM.
Max A.
+2  A: 

Hi knorv. Have you looked at Apache XmlSchema? I've never used it, but it seems like a good fit.

Matt Solnit
Just curious - why the downvote? Does someone have negative feedback about Apache XmlSchema?
Matt Solnit
I agree, it seems apt: http://ws.apache.org/commons/XmlSchema/schematutorial.html#reading though navigation isn't covered in detail.
13ren
It's used for this purpose inside of Apache CXF.
bmargulies
+2  A: 

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
Nick Veys
+1  A: 

Try org.apache.ws.jaxme.generator.sg.impl.JAXBSchemaReader. Here's a sample code snippet which may work:

org.apache.ws.jaxme.generator.sg.SchemaSG schema = JaxbSchemaReader.parse(schema);
org.apache.ws.jaxme.generator.sg.TypeSG types = schema.getTypes();
for (TypeSG type : types) {
    if (type.getComplexTypeSG() != null) {
           //do something here
    }
 }
Thimmayya
got a couple of downvotes on this answer ... anybody know if this answer is completely off-track?
Thimmayya