我正在使用JAXB来编组一些常规对象。我得到这样的输出:
<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>
我真的不希望xsi:type=
和xmlns:xsi=
显示在我的最终文档中,因为它不需要它。这可能吗?
另外 - 我设置@XmlElementWrapper(name="desiredskillslist")
将标签放在employeeDesiredSkills
周围,但如果可能的话,我宁愿不要有<employeeDesiredSkillsT>
标签 - 如果是的话就像名单一样,这很棒。当我注释ArrayList
?
答案 0 :(得分:1)
我真的不想要xsi:type =和 xmlns:xsi =出现在我的决赛中 文件,因为它不需要它。是 这可能吗?
我不确定为什么xsi:type会出现在你面前。正如您在下面的示例中所看到的,它通常不会出现。从下面的例子中你的模型有哪些不同的东西?
我宁愿没有 标签 - 如果它只是名单, 那太好了。
我相信您正在寻找的东西可以通过@XmlValue注释来实现。请参阅下面DesiredSkill类的name属性:
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>