2011-11-25 100 views
1

我是java新手(來自c#.net背景),正在嘗試上述示例以編組和解組。使用JAXB解組編碼

以下使用如由Mr.Blaise Doughan提到的上述技術下面 Marshalling a List of objects implementing a common interface, with JaxB

鏈路,我能夠封送java對象到XML。但是,當我保存這個XML,並嘗試解組XML回到java對象我得到的控制檯上執行以下操作:

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:2個 計數IllegalAnnotationExceptions @ javax.xml.bind.annotation.XmlElementRef註釋在兩個 的地方找到;一個就足夠了。此問題與以下 位置有關:at @ javax.xml.bind.annotation.XmlElementRef(name = ## default, required = true,type = class javax.xml.bind.annotation.XmlElementRef $ DEFAULT,namespace =)at public java.util.List Community.getPeople()在社區這個問題是 與以下位置相關:at @ javax.xml.bind.annotation.XmlElementRef(name = ## default, required = true ,類型= A類 javax.xml.bind.annotation.XmlElementRef $ DEFAULT,命名空間=)處社區公共 無效Community.setPeople(java.util.List的)....

注:我爲Class Boy和Class Girl創建了getters/setter來實現解組。

+0

看一看 「官方」 的例子:http://docs.oracle.com/javaee/ 5/tutorial/doc/bnbah.html – Kai

回答

3

看來,你可能已經標註的兩個getPeople和setPeople方法。 JAXB(以及其他Java EE技術)只需要註釋一個。

public class Community { 

    private List<Person> people; 

    @XmlElementRef 
    public List<Person> getPeople() { 
     return people; 
    } 

    public void setPeople(List<Person> people) { 
     this.people = people; 
    } 

} 

更多信息

+0

你好Mr.Blaise Doughan 代碼如下: 它幾乎與我所看到的相同在參考鏈接: http://stackoverflow.com/questions/4144296/marshalling-a-list-of-objects-with-jaxb/8266451#8266451 但現在我正在解編... JAXBContext jaxbContext = JAXBContext.newInstance(Community.class); \t \t \t Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); \t \t \t Community a = (Community) unmarshaller.unmarshal(new File("FILE path of XML")); 是的,我已註釋getPeople和setPeople方法。 –

+1

你好,先生, 你當場... !! 一切工作順利後我一直註釋僅適用於吸氣而不是制定者Person類 解組代碼: - JAXBContext jaxbContext = JAXBContext.newInstance(Community.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Community a = (Community) unmarshaller.unmarshal(new File("XMLFilePath")); \t List lstPeople = a.getPeople(); \t \t Iterator objIterator = lstPeople.iterator(); while(objIterator.hasNext()) { Person objPerson = objIterator.next(); \t \t \t \t System.out.println(objPerson.getName()); }

+0

回溯到什麼地方出了錯......控制檯輸出給的什麼是錯的線索...但由於即時通訊新的Java是不能夠了解編譯器提示... 感謝您的幫助Sir @Blaise Doughan。 將您的評論標記爲答案...! -jatin –

0

它更容易幫助,如果你表現出你的代碼...

的問題似乎是,你有getter和setter和混淆JAXB,因爲它不知道如何使用它們來解組XML。

嘗試使用FIELD訪問類型:

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlRootElement(name="Blubb", namespace=ServiceConstants.XML_NAMESPACE) 
public class Blubb implements Serializable { 

    @XmlElement(name="Bla", namespace=ServiceConstants.XML_NAMESPACE) 
    private Bla bla; 

    public Blubb() { 

    } 

    public void setBla(Bla bla) { this.bla = bla; } 

    public Bla getBla() { return this.bla; } 
} 
+0

Hello @hage代碼如下:它與我在參考鏈接中看到的幾乎相同:stackove rflow.com/questions/4144296/...但現在我正在解編... JAXBContext jaxbContext = JAXBContext.newInstance(Community.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Community a = (Community) unmarshaller.unmarshal(new File("FILE path of XML"));和是的我已註釋getPeople和setPeople方法都 –