I have a web application that needs to accept an XSD and a corresponding XML as input via form parameters.
I need to parse the XSD, identify the simple and complex types along with other necessary information, and represent this in some sort of a structural format (preferably a tree hierarchy)
This structural information will then be used to help parse the associated XML file, record by record into a CSV file.
The XSD will be using only a limited set of XSD constructs (namespaces, imports can be ignored for the time being)
What is the best way that I can go forward with this?
I have considered these options.
XSD-Java binding tools (XMLBeans, JAXB).
I can get a type hierarchy using these tools.
This would be the ideal scenario as I can create a new instance hierarchy based on the above type hierarchy (acts as an intermediate 'in memory' representation), populate the simple types (or attribute) with their respective values in the new instance of the type hierarchy as I parse through the XML and write it to the file. However, due to the high number of class files that are going to be generated on the system, this option cannot be considered. (For each XSD uploaded, the system would have to generate a set of class files and this is not acceptable)
Using XSOM, i can create a simple tree structure with two user defined types 'ComplexType' and 'SimpleType'
Set of simple types
Set of complex types
The problem here is I am not sure how I can go about parsing the XML and have an intermediate representation which could be committed to the CSV file.
DOM is not an option at all due to memory constraints.
What I have in mind for the intermediate structure to represent a single line of record in the CSV file is an array of key-value pairs. I could parse through the XSD, identify all the simple types (along with their position relative to the root) and initialize the array keys with the simple type names.
Further on, I could parse the XML (using SAX/StAX), populate the above array one simple type value at a time. I could make use of a stack to maintain the state information by pushing the element names. Once I encounter a type that is already present in the stack, it will identify the end of a line (or record) in the CSV file. The contents of the array will now be written to the CSV file, its contents will be reset (sanity check) and the parsing process will continue).
I was also thinking of replacing the array of key-value pairs with a type that extends HashMap, but I really do not need the flexiblity a Hashmap offers. Also, the ordering of elements might become an issue and I would have to move over to a Treemap (performance hit?). I feel that in the case of a deeply nested XML, hashing will provide a considerable performance improvement. So should I implement my own custom hashtable based fixed size array?
Awaiting your feedback and comments. I have been burning my head for the past few days trying to figure out the most efficient and scalable way of doing this. If you could provide me with any better alternative approaches, it would be really helpful.