Parsing XML is a fairly common task and it is relativily easy because you can use quite a lot of parsers. However, parsing a XSD Schema file is not an easy task. You can parse it as a regular XML, but transform all elements and attributes into java objects are rather difficult. You can use tools, such as XSOM, but it doesn’t seem to be maintained anymore.
As a consequence of that, I decided to develop a proof-of-concept solution using Xerces2. My only goal was to develop a solution that transforms xs:element into XSDElement java objects and xs:attribute into objects XSDAttribute.
In the XSDElements we will save the following data:
1 2 3 4 5 6 7 8 9 |
private String name; private XSDElement parent; private List<XSDAttribute> attributes = new ArrayList<XSDAttribute>(); private List<XSDElement> children = new ArrayList<XSDElement>(); private int minOcurrs; private boolean maxOcurrsUnbounded; private int maxOcurrs; private String type; private String defaultValue; |
and in the XSDAttributtes we will save the following data:
1 2 3 4 5 |
private String name; private boolean required; private String type; private List<String> options = new ArrayList<String>(); private String defaultValue; |
To parse a XSD Schema file using Xerces2 you need to do:
1 2 3 4 5 6 7 |
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); XSImplementation impl = (XSImplementation) registry.getDOMImplementation("XS-Loader"); XSLoader schemaLoader = impl.createXSLoader(null); LSInput input = new DOMInputImpl( ); input.setByteStream(inputStream); // XSD Scheam file in an InpuStream XSModel model = schemaLoader.load(input); |
I developed a very simple solution in which I was able to parse only a few elements or only one element and its children. You can find the complete code in GitHub XSDParser repository. With XSDParserTest you can parse an example of XSD Schema file.