2017-07-28 25 views
0

如何將下面的一段xsd轉換爲java pojo。我嘗試使用JAXB項目方式使用eclipse轉換它,但它給我錯誤(Property "Value" is already defined. Use <jaxb:property> to resolve this conflict.)。我認爲它是因爲我有名稱=「價值」,並在某處發生衝突。問題,同時將xsd轉換爲Java POJO當屬性名稱=「值」時

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

幫助表示讚賞!

回答

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

現在用傑克遜API時春天來解讀呢?這是行不通的。我在POST中發送XML到春天休息控制器,它不填充任何東西。 – ProgrammerBoy

+1

@ProgrammerBoy這應該起作用,所以如果沒有,那麼你做錯了什麼。既然你沒有顯示你所做的事情,我們不可能確定你做錯了什麼。這個問題已被回答。如果您還有其他問題,請創建一個新問題,幷包含所有相關信息,例如POST的實際有效負載,JABX註釋類以及處理POST的Spring MVC方法。 – Andreas

+0

感謝您付出努力回答它......我很感激。我得到「JSON解析錯誤:無法識別的字段」「」錯誤信息。稍後我會針對細節創建一個單獨的問題。 – ProgrammerBoy

相關問題