0

Is there any way to assign different names to the same type when using @XmlElements? I started off with just using @XmlElement. Did some reading and found @XmlElementWrapper and the @XmlElements but still have not been able to get my desired output. I realize that I could just make different data types for the 2 but it would be sweet if I could just do this with annotations.

Current Iteration:

@XmlRootElement(name = "Root")
public class XmlTest {

    @XmlElementWrapper(name="ContactInformation")
    @XmlElements({
            @XmlElement(name="Name"),
            @XmlElement(name="LogicalOwner")
    })
    public List<String> contactInformation;
    ...
        contactInformation = new ArrayList<>();
        contactInformation.add("should be inside name");
        contactInformation.add("should be insde of owner");
    ...

Current Output:

<Root>
    <ContactInformation>
        <LogicalOwner>should be inside name</LogicalOwner>
        <LogicalOwner>should be insde of owner</LogicalOwner>
    </ContactInformation>
</Root>

Desired Output:

<Root>
    <ContactInformation>
        <Name>should be inside name</Name>
        <LogicalOwner>should be insde of owner</LogicalOwner>
    </ContactInformation>
</Root>
gonzo
  • 2,103
  • 1
  • 15
  • 27

1 Answers1

0

You could try making a class that stores the fields in them, and then serialize that class.

@XmlRootElemnt(name="root")
Class ContactInformation{

    private String name;

    @XmlElement(name="Name")
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

I'm used to using Gson.toJson(), so I'm not much help on serializing to XML; hopefully this points you in the right direction though.

ZachCleaver
  • 38
  • 1
  • 5
  • Yeah... I was hoping to avoid creating another class. But this would work. This was going to be my plan B if I could not figure out a way to do it all in one class. – gonzo Jun 10 '16 at 20:40