Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
属性name=“value”时将xsd转换为java pojo时出现问题_Java_Xsd_Jaxb - Fatal编程技术网

属性name=“value”时将xsd转换为java pojo时出现问题

属性name=“value”时将xsd转换为java pojo时出现问题,java,xsd,jaxb,Java,Xsd,Jaxb,如何将下面的xsd转换为JavaPOJO。我尝试使用JAXB项目的方式使用eclipse对其进行转换,但它的giving-me-error属性值已经定义。使用jaxb:property>解决此冲突。。我想这是因为我有name=value,它在某个地方相互冲突 <xs:complexType name="demo"> <xs:simpleContent> <xs:extension base="xs:string"> <

如何将下面的xsd转换为JavaPOJO。我尝试使用JAXB项目的方式使用eclipse对其进行转换,但它的giving-me-error属性值已经定义。使用jaxb:property>解决此冲突。。我想这是因为我有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);
    }
}
输出

这是元素内容
当我在春季使用jacksonapi解压它时,这是不起作用的。我正在向SpringREST控制器发送POST中的xml,但它没有填充任何内容。@ProgrammerBoy这应该可以工作,所以如果不能,那么您就做错了。既然你没有展示你所做的,我们就不可能确定你做错了什么。这个问题已经回答了。如果您还有其他问题,请创建一个新的问题,并包括所有相关信息,例如文章的实际有效负载、JABX注释类以及处理文章的Spring MVC方法。感谢您为回答此问题付出的努力……我非常感谢。我收到JSON解析错误:无法识别的字段错误消息。稍后我将创建一个单独的问题,并提供详细信息。处理XML时JSON解析错误?这应该是个线索,你不觉得吗。客户是否在帖子上给出了正确的内容类型?您在Spring注释中提供了正确的内容类型吗?