0

Our applications uses java classes generated from XML Schema Definition (XSD). I need to use HashMap for storing key, value pairs.

The Result class should be something like this from xsd

public class Employee{ private String name; private Map<String, Object> dataMap; } 

Tried schemagen, jaxbcontext etc, but still unable to do it successfully . Tried below in XSD but getting "List" in the generated java class (instead of map). Using JDK 1.8

<xs:complexType name="employee"> <xs:sequence> <xs:element name="dataMap"> <xs:complexType> <xs:sequence> <xs:element name="entry" minOccurs="0" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="key" minOccurs="0" type="xs:string"/> <xs:element name="value" minOccurs="0" type="xs:anyType"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="value" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:complexType> 

Appreciate any help/suggestions

8
  • 1
    I think you should look at XmlAdapter usage. See linked question here Commented Feb 28, 2024 at 23:20
  • 1
    I was able to achieve it using jaxb:bindings (<jaxb:baseType name="java.util.Map&lt;String,Object&gt;" />) and can/will post full solution shortly. Meanwhile I wish with solution if I can somehow initialize the HashMap as empty when class is generated using xsd (currently the getter returns null) Commented Feb 29, 2024 at 14:58
  • Like the list is initialized with new list when getter is called for the first time ? Commented Feb 29, 2024 at 18:19
  • yes, but trying to do this with Map as well Commented Feb 29, 2024 at 18:50
  • If you dont find a solution, please create issue here github.com/highsource/jaxb-tools We could work on new xjc plugin for that kind of field initialization Commented Feb 29, 2024 at 22:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.