Search code examples
xmlrestlet

How to control the xml element label generated by xstream?


I use xstream to convert object to xml.

public class XXXInfo
{
    private String id;

    private String name;

    private YYYInfo yyy;
}

the member variable's name is correctly written to xml file.

but the element label for the outermost layer of the xml comes with a prefix of my package name.

How to modify it to what I want it?


Solution

  • You should use the annotation to control this.

    @XStreamAlias("names you want")
    public class XXXInfo
    {
        private String id;
    
        private String name;
    
        private YYYInfo yyy;
    }
    

    and then remember to call the XStream.processAnnotations(XXXInfo.class) to make it effect.

    or you can also call the XStream.autodetectAnnotations(true) to enable the annotation detection function.

    Good Luck!