如何将下面的xsd转换为java pojo。我尝试使用eclipse转换它的JAXB项目方式,但它给了我错误(Property "Value" is already defined. Use <jaxb:property> to resolve this conflict.
)。我认为这是因为我有名字=“价值”而且它在某处有冲突。
<xs:complexType name="demo">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="value" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
非常感谢帮助!
答案 0 :(得分:1)
表示该复杂类型的Java类可能是这样的:
@XmlType(name = "demo")
public class Demo {
private String valueAttr;
private String valueContent;
@XmlAttribute(name = "value")
public String getValueAttr() {
return this.valueAttr;
}
public void setValueAttr(String valueAttr) {
this.valueAttr = valueAttr;
}
@XmlValue
public String getValueContent() {
return this.valueContent;
}
public void setValueContent(String valueContent) {
this.valueContent = valueContent;
}
}
类名,字段名和方法名可以更改为您想要的名称,因为XML名称在注释中明确给出。
要查看它的工作原理,请使用:
@XmlRootElement
public class Test {
@XmlElement
private Demo demo;
public static void main(String[] args) throws Exception {
Demo demo = new Demo();
demo.setValueAttr("this is the attr value");
demo.setValueContent("this is the element content");
Test test = new Test();
test.demo = demo;
JAXBContext jaxbContext = JAXBContext.newInstance(Test.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(test, System.out);
}
}
输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<test>
<demo value="this is the attr value">this is the element content</demo>
</test>