Serializing java objects with respect to xml schema loaded at runtime
        Posted  
        
            by kohomologie
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by kohomologie
        
        
        
        Published on 2010-02-24T08:42:31Z
        Indexed on 
            2010/06/13
            7:52 UTC
        
        
        Read the original article
        Hit count: 975
        
I call an XML document three-layered if its structure is laid out as following: the root element contains some container elements (I'll call them entities), each of them has some simpleType elements inside (I'll call them properties). Something like that:
<data>
 <spaceship>
    <number>1024</number>
    <name>KTHX</name>
 </spaceship>
 <spaceship>
    <number>1624</number>
    <name>LEXX</name>
 </spaceship>
 <knife>
    <length>10</length>
 </knife>
</data>
where spaceship is an entity, and number is a property. 
My problem is stated below:
Given
schema: an arbitrary xsd file describing a three-layered document, loaded at runtime.
xmlDocument: an xml document conforming to the schema.
Create
A Map<String, Map <String, Object>> containing data from the xmlDocument, where first key corresponds to entity, second key correponds to this entity's property, and the value corresponds to this property's value, after casting it to a proper java type (for example, if the schema sets the property value to be xs:int, then it should be cast to Integer).
What is the easiest way to achieve this result with existing libraries?
P. S. JAXB is not really an option here. The schema might be arbitrary and unknown at compile-time. Also I wish to avoid an excessive use of reflection (associated with converting the beans to maps). I'm looking for something that would allow me to make the typecasts while xml is being parsed.
© Stack Overflow or respective owner