Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java methods for interrogating XSD files

Tags:

java

xml

xsd

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.

like image 518
DUFF Avatar asked Oct 29 '11 18:10

DUFF


People also ask

What is the use of XSD file in Java?

xsd is the XML schema you will use as input to the JAXB binding compiler, and from which schema-derived JAXB Java classes will be generated. For the Customize Inline and Datatype Converter examples, this file contains inline binding customizations.

What is XSD validation?

The WSRR web user interface validates each definition file when starting, and when new or updated definition files are loaded. This is done according to the definition XML Schema Definition (XSD).

How can an element be defined within an XSD?

Each element definition within the XSD must have a 'name' property, which is the tag name that will appear in the XML document. The 'type' property provides the description of what type of data can be contained within the element when it appears in the XML document.


1 Answers

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.

like image 71
Bill Avatar answered Sep 28 '22 03:09

Bill