2012-04-03 85 views
0

這是我的XML文件JAXB:請驗證的XML結構到Java的JAXB

<BADFM> 
<Given> 
<Ord> 
<Bag IDC="DM" /> 
</Ord> 
</Given> 
</BADFM> 

這是我的分析器類

import java.io.File; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.Unmarshaller; 

public class Test { 
    public static void main(String args[]) throws Exception { 

     File file = new File("D:\\BADML.xml"); 
     JAXBContext jaxbContext = JAXBContext 
       .newInstance(MyMessage.class); 
     Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
     MyMessage authentifyResult = (MyMessage) jaxbUnmarshaller 
       .unmarshal(file); 
     System.out.println(authentifyResult.getGiven().getOrd().getBag().getIDC()); 

    } 
} 

這是MyMessage

import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement(name="BADFM") 
public class MyMessage 
{ 
@XmlElement(name="Given") 
private Given given; 

public Given getGiven() { 
    return given; 
} 

public void setGiven(Given given) { 
    this.given = given; 
} 


} 

這是Given.java

import javax.xml.bind.annotation.XmlElement; 

public class Given { 
    private Ord ord; 
    @XmlElement(name = "Ord") 
    public Ord getOrd() { 
     return ord; 
    } 
    public void setOrd(Ord ord) { 
     this.ord = ord; 
    } 
} 

這是Ord.java

import javax.xml.bind.annotation.XmlElement; 

public class Ord { 

    private Bag bag; 
    @XmlElement(name="Bag") 
    public Bag getBag() { 
     return bag; 
    } 

    public void setBag(Bag bag) { 
     this.bag = bag; 
    } 

} 

這是Bag.java

import javax.xml.bind.annotation.XmlAttribute; 

public class Bag { 
     @XmlAttribute(name="IDC") 
    private String IDC ; 

    public String getIDC() { 
     return IDC; 
    } 
    @XmlAttribute(name="IDC") 
    public void setIDC(String IDC) { 
     IDC = IDC; 
    } 

} 

,當我跑的是我得到

Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions 
Class has two properties of the same name "IDC" 
    this problem is related to the following location: 
     at public java.lang.String Bag.getIDC() 
     at Bag 
     at public Bag Ord.getBag() 
     at Ord 
     at public Ord Given.getOrd() 
     at Given 
     at public Given MyMessage.getGiven() 
     at MyMessage 
    this problem is related to the following location: 
     at private java.lang.String Bag.IDC 
     at Bag 
     at public Bag Ord.getBag() 
     at Ord 
     at public Ord Given.getOrd() 
     at Given 
     at public Given MyMessage.getGiven() 
     at MyMessage 

    at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(Unknown Source) 
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(Unknown Source) 
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source) 
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(Unknown Source) 
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source) 
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at javax.xml.bind.ContextFinder.newInstance(Unknown Source) 
    at javax.xml.bind.ContextFinder.find(Unknown Source) 
    at javax.xml.bind.JAXBContext.newInstance(Unknown Source) 
    at javax.xml.bind.JAXBContext.newInstance(Unknown Source) 
    at Test.main(Test.java:11) 

回答

1

您需要使用@XmlAttributeBag類的IDC變量。一旦你做了這個改變,你在頂部引用的XML將會起作用。

至於你當前的代碼,它期待在XML的樣子:

<Bag> 
    <IDC>DM</IDC> 
</Bag> 

你可以很容易地看到你的類被填充的所有屬性字段期待什麼類型的XML,然後編組對象到文件。

更新

你應該總是聲明你的屬性爲私有,如果你將擁有正確命名的getter和setter方法。否則,JAXB將拋出錯誤Class has two properties of the same name

當聲明class屬性爲@XmlAttribute時,還應該將註釋放在getter上,以便JAXB認爲您不希望@XmlAttribute@XmlElement具有相同的名稱。

public class Bag { 
    private String IDC ; 

    @XmlAttribute(name="IDC") 
    public String getIDC() { 
     return IDC; 
    } 

    public void setIDC(String IDC) { 
     this.IDC = IDC; 
    } 
} 
+0

由於很多,所以如果我使用@XmlAttribute爲IDC將代碼將用於此XML工作 <袋IDC = 「DM」/> Pawan 2012-04-03 17:33:09

+0

是的。我已經更新了答案,以表明這一點。 – 2012-04-03 17:52:40

+0

嗨,感謝您的及時迴應,當我運行,我得到2 IllegalAnnotationExceptions計數,我已更新我的問題,請告訴我什麼是錯誤 – Pawan 2012-04-03 18:10:32