English 中文(简体)
Getting an object representation of an .XSD file in Java
原标题:
  • 时间:2009-11-12 22:38:32
  •  标签:
  • java
  • xml
  • xsd

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
问题回答

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

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
    }
 }

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.

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





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签