Storing List<String> with XStream with defined names
Asked Answered
O

3

9

I want to store some class to xml:

    XStream xstream = new XStream(new DomDriver());

    List<String> modules = new ArrayList<String>();
    modules.add("mod1");
    modules.add("mod2");
    ModulesConfig modulesConfig = new ModulesConfig(modules);

xstream.processAnnotations(ModulesConfig.class);
log.info(xstream.toXML(modulesConfig));

Class ModulesConfig:

@XStreamAlias("modules-config")
public class ModulesConfig {

    @XStreamAlias("moduleName")
    private List<String> moduleNames;

    public ModulesConfig(List<String> moduleNames) {
        this.moduleNames = moduleNames;
    }

    public List<String> getModuleNames() {
        return moduleNames;
    }

    public void setModuleNames(List<String> moduleNames) {
        this.moduleNames = moduleNames;
    }
}

Result:

<modules-config>
  <moduleName>
    <string>mod1</string>
    <string>mod2</string>
  </moduleName>
</modules-config>

But I want elements were named "moduleName". How to configure xstream or what annotations I must to add to class? I want such result:

<modules-config>
  <moduleNames>
    <moduleName>mod1</moduleName>
    <moduleName>mod2</moduleName>
  </moduleNames>
</modules-config>
Odellodella answered 16/1, 2012 at 16:17 Comment(2)
possible duplicate of customising serialisation of java collections using xstreamActin
Read thisRefrigeration
I
4

This won't have the exact result as you requested but seems cleaner:

@XStreamImplicit(itemFieldName="moduleName")
private List<String> moduleNames;

The produced result will be:

<modules-config>
  <moduleName>mod1</moduleName>
  <moduleName>mod2</moduleName>
</modules-config>
Ibis answered 16/1, 2012 at 17:0 Comment(1)
Yes, but how to get the <moduleNames> tag?Earthshine
S
2

You can define the moduleNames as a Bean,Like this

@XStreamAlias("moduleNames")

public class ModuleNames{

    @XStreamImplicit(itemFieldName="moduleName")
    private List<String> moduleNames;

    public List<String> getModuleNames() {
        return moduleNames;
    }

    public void setModuleNames(List<String> moduleNames) {
        this.moduleNames = moduleNames;
    }
}


//then  add the ModuleNames as The List<String> and hide the List Tag

@XStreamAlias("modules-config")

public class ModulesConfig {

    @XStreamAlias("moduleNames")
    private ModuleNames moduleNames;

    public ModulesConfig(List<String> moduleNames) {
        this.moduleNames = moduleNames;
    }

    public List<String> getModuleNames() {
        return moduleNames.getModuleNames();
    }

    public void setModuleNames(List<String> moduleNames) {
        this.moduleNames.set(moduleNames);
    }
}
Sascha answered 13/11, 2013 at 4:34 Comment(0)
F
0

I don't really know the XSteam framework but I think it will be pretty similar to JAXB. There you could do something like this.

Class ModuleConfig

@XmlRootElement(name="moduleConfig")
@XmlAccessorType(XmlAccessType.FIELD)
public class ModuleConfig {

   List<ModuleName> moduleNames;
   public ModuleConfig() {
       moduleNames = new ArrayList<ModuleName>();
       moduleNames.add(new ModuleName("mod1"));
       moduleNames.add(new ModuleName("mod2"));
   }

}

Class ModuleName

@XmlRootElement(name="moduleName")
@XmlAccessorType(XmlAccessType.FIELD)
public class ModuleName {

     private String moduleName;

     public ModuleName() { }

     public ModuleName(String moduleName) {
         this.moduleName = moduleName;
     }

     public String getModuleName() {
        return moduleName;
     }

}

Wich will generate an output like this

<moduleConfig>
  <moduleNames>
   <moduleName>mod1</moduleName>
  </moduleNames>
  <moduleNames>
   <moduleName>mod2</moduleName>
  </moduleNames>
 </moduleConfig>
Facula answered 16/1, 2012 at 17:5 Comment(1)
No, I need one moduleNames node.Odellodella

© 2022 - 2024 — McMap. All rights reserved.