Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert XSD to tree structure with Java

Tags:

java

xsd

I want to generate documentation for XML schemas.

My goal is to analyze the xsd file and to display it as a tree structure (with all complex / anonymous types resolved). Furthermore I need to annotate all items in that tree with their cardinality (as defined by the schema).

The following small example might help to clarify my problem.

a) the xsd file:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="client" type="clientType" />
    <xs:complexType name="clientType">
        <xs:sequence minOccurs="1" maxOccurs="1">
            <xs:element name="first_name"/>
            <xs:element name="last_name"/>
            <xs:element name="address" type="addressType" 
                        minOccurs="1" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="addressType">
        <xs:sequence>
            <xs:element name="street"/>
            <xs:element name="number" minOccurs="0" maxOccurs="1"/>
            <xs:element name="city"/>
            <xs:element name="zipcode"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

b) Output I'd like to see:

client [1]
  first_name [1]
  last_name [1]
  address [1..n]
    street [1]
    number [0..1]
    city [1]
    zipcode [1]

Does anybody know a java based solution for this problem? Preferably based on Eclipse Schema Infoset, but I'm happy to use other libraries as well.

like image 875
Johannes K. Lehnert Avatar asked Nov 14 '22 20:11

Johannes K. Lehnert


1 Answers

XSOM can normalize an XSD into a comprehensible data structure that you can loop over and print out.

like image 142
jiggy Avatar answered Dec 16 '22 18:12

jiggy