views:

2478

answers:

6

I've been looking around for a way to programmatically parse XSD files to generate an XML mapping so that I can take any XSD file and create a parser that will parse any XML file that conforms to it, and my google-fu has been coming up short.

XSOM looks promising, as it will parse an XSD file and make all its attributes available in a straight forward way.

Are there any other options or standard libraries available that will produce an XML file parser from an XSD file?

[Conclusion] Thanks everyone for your responses, they were a real help. I was able to write implementations using JAXP, Eclipses EMF and in XSOM that all worked as desired.

JAXP was very straight forward and easy to learn and do. EMF was actually a pain to get going properly, there were so many jars that had to be included for it to work standalone that I would not recommend it. XSOM was even simpler than the JAXP implementation, so I went with it in the end.

+3  A: 

Yes, there's a standard way.

You're looking for JAXB.

Michael van der Westhuizen
I'm having trouble finding any reference to JAXB being able to parse XSD files. Could you give me an example or a hint as to how it does this?i.e.JAXB.parse(new File("myxsd.xsd"));
Andrew
You're looking for the SchemaFactory class. You load the schema using the schema factory, then set it in your XMLStreamReader, and it gets used. Have a look at http://myarch.com/using-xml-validation-framework-with-web-services to get some ideas.If you just want to generate bindings, don't write your own code - JAXB and JAX-WS can generate your bindings directly from your XSD. Have a look at the Java EE tutorial at http://java.sun.com/javaee/5/docs/tutorial/doc/ to get examples of how this is done.
Michael van der Westhuizen
+2  A: 

If you are looking at something for binding the XML to Java objects, then there are some choices:

jsight
These require that I define the XSD beforehand to generate the marshallers. Is there a way to read an XSD and create the at marshallers at runtime?
Andrew
I think that would involve some manual coding on your part regardless of how you went about it. How would it know how to map the xml pieces into your object schema at runtime without some sort of mapping definition? Perhaps you are looking for something dynamic (more like Groovy's DOMCategory)?
jsight
Using a Schema to object compiler at runtime wouldn't really make sense. That type of technology is meant to facilitate creating objects that you can code against to marshall and unmarshall to and from XML. It sounds like the original poster should investigate XOM, JDOM, or even the W3C DOM in the JDK.
laz
@Andrew I believe all Java command-line tools can be called at runtime, so you can call xjc (or schemagen) in that way. I have used this with javac (in 1.6), and though it's a little involved, using the javax.tools package. Not a complete answer, but a starting point for googling.
13ren
A: 

My personal favourite is Apache's XMLBeans.
It can create a Jar file containing parser, writer and verifier based on an XSD.

Stroboskop
It seems convoluted to run a script to generate a jar then pull it in at runtime. This is just if you have a known XSD file.
Andrew
It's not supposed to work for "on the fly" parsing. The created beans are really only useful at compile time.
Stroboskop
+1  A: 

I've used the Eclipse XSD libraries to parse XML Schema in the past. http://eclipse.org/modeling/mdt/?project=xsd, and found it very straighforward to use There are a ton of jar files that come down, but you will only need 5 of them at runtime to do the parsing.

I used that one too long time ago. It was pretty good ... my biggest problem was my lack of understanding of schemas back then (something about abstract vs concrete schema if I remember correctly). When I look at "http://eclipse.org/modeling/mdt/?project=xsd" now, it looks like bunch of 3/4-letter acronyms put together ;-)
Peter Štibraný
A: 

When parsing a XSD file through XSOM, does anybody know if it is posible to find elements that exist inside elements, such as the example given below? I am only able to obtain one element when calling this.xsSchema.getElementDecls() on the XSD file below.

Brad
A: 

Here is the schema without the "<" to show. xsd:element name="Outside" xsd:complexType xsd:choice xsd:element name="Inside" type="xsd:string" xsd:element xsd:choice xsd:complexType xsd:element xsd:schema

Brad