开发者

JAXB suppress XSI and xmlns

开发者 https://www.devze.com 2023-01-17 09:10 出处:网络
I am using JAXB to marshall some groovy objects. I am getting output like this: <desiredskillslist>

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">
     <name>C Development</name>
 </employeeDesiredSkills>
 <employeeDesiredSkills xsi:type="employeeDesiredSkill" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <name>Perl Development</name>
 </employeeDesiredSkills>
 <employeeDesiredSkills xsi:type="employeeDesiredSkill" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <name>Java Development</name>
 </employeeDesiredSkills>
 <employeeDesiredSkills xsi:type="employeeDesiredSkill" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <name>Database Design</name>
 </employeeDesiredSkills>
 <employeeDesiredSkills xsi:type="employeeDesiredSkill" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <name>Grails Development</name>
 </employeeDesiredSkills>

开发者_JS百科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?


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消