user6677603
user6677603

Reputation:

How to read xml with HashMap inside the class via XStream

Here are my class fields.

 @XStreamAlias("OrgEntity")
    public class OrgEntity implements Serializable {

    private static final long serialVersionUID = 2L;
    private static final int MAX_SHARE_PERCENTAGE = 100;
    private static final byte MIN_ORG_NAME_LENGTH = 2;
    public static Map<String, OrgEntity> allOrgMap;
    @XStreamAlias("entityName")
    @XStreamAsAttribute
    private String entityName;
    private OrgType type;
    private float sharesPortionHeldBy;
    private double authorisedCapital;
    private String parentCompanyName;
    private Human ceo;

    private Map<ShareHolder, Float> shareHoldersMap;
.... 
}

This is the xml that I need to read:

<OrgEntity entityName="nonParentCompany">
  <type>ZAO</type>
  <sharesPortionHeldBy>13.0</sharesPortionHeldBy>
  <authorisedCapital>1000000.0</authorisedCapital>
  <ceo surname="Narishkin" firstName="Denis" middleName="Ven"/>
  <shareHoldersMap>
    <entry>
      <ShareHolder surname="Trump" firstName="Don" middleName="J">
        <shareHolderPortfolioMap>
          <shareHolderAsset>
            <entry>
              <string>nonParentCompany</string>
              <float>5.0</float>
            </entry>
          </shareHolderAsset>
          <outer-class reference="../.."/>
        </shareHolderPortfolioMap>
        <currCom reference="../../../.."/>
        <sharesNumberCurrComp>5.0</sharesNumberCurrComp>
      </ShareHolder>
      <float>5.0</float>
    </entry>
    <entry>
      <ShareHolder surname="Smith" firstName="Adam" middleName="A">
        <shareHolderPortfolioMap>
          <shareHolderAsset>
            <entry>
              <string>nonParentCompany</string>
              <float>8.0</float>
            </entry>
          </shareHolderAsset>
          <outer-class reference="../.."/>
        </shareHolderPortfolioMap>
        <currCom reference="../../../.."/>
        <sharesNumberCurrComp>8.0</sharesNumberCurrComp>
      </ShareHolder>
      <float>8.0</float>
    </entry>
  </shareHoldersMap>
</OrgEntity>

How can I read this xml through XStream? I tried to use the code below but it failed. The exeption that I receive is

Exception in thread "main" com.thoughtworks.xstream.InitializationException: No field "shareHoldersMap" for implicit collection

What should I do in order to be able to read the xml?

public static void main(String[] args) {
        OrgEntity o = DataForRunners.createDataForBasicFile15OrgsWithShareHoldingsUpdateForXML().get(0);
        String xmlOrgObj = "src/main/java/com/.... ";
        new XStreamDecorator<OrgEntity>(o, xmlOrgObj);
        XStream xstream = new XStream(new DomDriver());
        xstream.alias("OrgEntity", OrgEntity.class);
        xstream.alias("entityName", OrgEntity.class);
        xstream.alias("type", OrgType.class);
        xstream.alias("ceo", Human.class);
        xstream.addImplicitCollection(HashMap.class, "shareHoldersMap");
        InputStream in = null;
        try {

            in = new FileInputStream(xmlOrgObj);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        OrgEntity var =(OrgEntity) xstream.fromXML(in);
        System.out.println(var);
    }

Upvotes: 1

Views: 227

Answers (1)

mhasan
mhasan

Reputation: 3709

In XStream 1.4.5 and above you can use NamedMapConverter.

So in your case you can try something like this:

XStream xstream = new XStream();
NamedMapConverter namedMapConverter = new NamedMapConverter(xstream.getMapper(), "entry", "key", ShareHolder.class, "value", Float.class);
xstream.registerConverter(namedMapConverter);

Upvotes: 1

Related Questions