gpt4 book ai didi

java - 当属性名称为 ="value"时,将 xsd 转换为 java pojo 时出现问题

转载 作者:行者123 更新时间:2023-12-02 12:29:01 26 4
gpt4 key购买 nike

如何将下面的 xsd 转换为 java pojo.我尝试使用 JAXB 项目方式,使用 eclipse 来转换它,但它给了我错误(Property "Value"is already Defined。使用 解决此冲突。 )。我认为这是因为我有 name="value"并且它在某个地方发生冲突。

 <xs:complexType name="demo">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="value" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>

感谢帮助!

最佳答案

表示该复杂类型的 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>

关于java - 当属性名称为 ="value"时,将 xsd 转换为 java pojo 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45376458/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com