tags:

views:

89

answers:

1

I am using JAXB to marshall some groovy objects. I am getting output like this:

<desiredskillslist>
 <employeeDesiredSkills xsi:type="employeeDesiredSkill" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
     <name>C Development</name>
 </employeeDesiredSkills>
 <employeeDesiredSkills xsi:type="employeeDesiredSkill" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
     <name>Perl Development</name>
 </employeeDesiredSkills>
 <employeeDesiredSkills xsi:type="employeeDesiredSkill" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
     <name>Java Development</name>
 </employeeDesiredSkills>
 <employeeDesiredSkills xsi:type="employeeDesiredSkill" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
     <name>Database Design</name>
 </employeeDesiredSkills>
 <employeeDesiredSkills xsi:type="employeeDesiredSkill" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
     <name>Grails Development</name>
 </employeeDesiredSkills>

I really dont want the xsi:type= and xmlns:xsi= to show up in my final document, since it wont need it. Is this possible?

Also - I set up an @XmlElementWrapper(name="desiredskillslist") to put the tags around the employeeDesiredSkills, but if at all possible, I'd ratherjust not have the <employeeDesiredSkillsT> tags at all - if itw as just the list of names, that'd be great. Why is it coming out like this when I annotated the ArrayList?

A: 

I really dont want the xsi:type= and xmlns:xsi= to show up in my final document, since it wont need it. Is this possible?

I'm not sure why xsi:type is showing up for you. As you can see in the example below it generally does not appear. What types of things are different about your model from the example below?

I'd ratherjust not have the tags at all - if itw as just the list of names, that'd be great.

I believe what you are looking for can be achieved with the @XmlValue annotation. See the name property on the DesiredSkill class below:

import java.util.*;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Root {

    private List<DesiredSkill> employeeDesiredSkills = new ArrayList<DesiredSkill>();

    public List<DesiredSkill> getEmployeeDesiredSkills() {
        return employeeDesiredSkills;
    }

    public void setEmployeeDesiredSkills(List<DesiredSkill> employeeDesiredSkills) {
        this.employeeDesiredSkills = employeeDesiredSkills;
    }

}

and:

import javax.xml.bind.annotation.XmlValue;

public class DesiredSkill {

    private String name;

    @XmlValue
    public String getName() {
        return name;
    }

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

}

Then the following code:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        Root root = new Root();

        DesiredSkill cDev = new DesiredSkill();
        cDev.setName("C Development");
        root.getEmployeeDesiredSkills().add(cDev);

        DesiredSkill perlDev = new DesiredSkill();
        perlDev.setName("Perl Development");
        root.getEmployeeDesiredSkills().add(perlDev);

        JAXBContext jc = JAXBContext.newInstance(Root.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }
}

Will produce:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <employeeDesiredSkills>C Development</employeeDesiredSkills>
    <employeeDesiredSkills>Perl Development</employeeDesiredSkills>
</root>
Blaise Doughan
I will try the rest of what you posted - but I think the xsi was coming because I originally had my lists declared as List somelist = new ArrayList() without the types explicitly declared.
Derek
If you don't explicitly declare the type in the List property then you can also do it on the annotation @XmlElement(type=EmployeeDesiredSkill.class).
Blaise Doughan