JAXB suppresses XSI and xmlns

I use JAXB to sort some groovy objects. I get the output as follows:

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

I really do not want to xsi:type=, and xmlns:xsi=displayed in my final document, as it does not need. Is it possible?

Also, I set up @XmlElementWrapper(name="desiredskillslist")to put tags around employeeDesiredSkills, but if at all possible, I would rather simply not have tags <employeeDesiredSkillsT>at all, if it was just a list of names, that would be great. Why does this happen when I annotated ArrayList?

+3
source share
1 answer

xsi: type = xmlns: xsi = , . ?

, xsi: . , . ?

  - , .

, , , @XmlValue. . name DesiredSkill :

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;
    }

}

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;
    }

}

:

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);
    }
}

:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <employeeDesiredSkills>C Development</employeeDesiredSkills>
    <employeeDesiredSkills>Perl Development</employeeDesiredSkills>
</root>
+1

Source: https://habr.com/ru/post/1766081/


All Articles