I have a set of xsd files for different data types. In the Java world, what is the best way to generate a list of the properties of the types?
e.g. with these two files.
file: customer.xsd
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="customer">
<xs:complexType>
<xs:sequence>
<xs:element name="number" type="xs:integer"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
file: order.xsd
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="customer">
<xs:complexType>
<xs:sequence>
<xs:element name="orderid" type="xs:integer"/>
<xs:element name="customer" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I’d like to do two things
1. a Java application which reads in the XSD and processes then (somehow?). So when you run the program it can print the properties out
> java -jar printtypes.jar -f customer.xsd
> number : Integer
> name : String
> address : String
2. some kind of transformation which generates a new file
file: customer.properties
<propertylist>
<prop>
<name> orderid </name>
<type> integer </type>
</prop>
<prop>
<name> customer </name>
<type> string</type>
</prop>
</propertylist>
I tried to implement the program in (1) above using java reflection to interrogate Java classes generated by JAXB.
This created an instance of the class and interrogated the values and fields, but it does not work where values are composed of a sequence that is empty. There is no way to get back to the original type of the value due to type erasure. You end up with an empty ArrayList of something, but you don’t know what.
I’m from the C++ work so I’m a bit lost with all this Java technology at the moment. My Google powers have failed me – most of the JAVA/XSD posts I’ve seen talk about validation which is not what I want to do.
You might want to look into XSOM, its a project that will ingest your XML schema and produce objects that you can traverse and produce your desired results.
http://xsom.java.net/userguide.html
Parsing schema by hand can be really tricky because there can be different ways to say basically the same thing.