2014-09-19 28 views
1
import java.util.ArrayList; 
    import java.util.HashMap; 
    import java.util.List; 

    import javax.xml.bind.JAXBContext; 
    import javax.xml.bind.Marshaller; 
    import javax.xml.bind.annotation.XmlAccessType; 
    import javax.xml.bind.annotation.XmlAccessorType; 
    import javax.xml.bind.annotation.XmlElement; 
    import javax.xml.bind.annotation.XmlRootElement; 
    import javax.xml.bind.annotation.XmlType; 



    public class JavaToXMLDemo { 
     public static void main(String[] args) throws Exception { 
    JAXBContext context = JAXBContext.newInstance(Employee.class); 

    Marshaller m = context.createMarshaller(); 
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 

    Employee object = new Employee(); 
    object.setCode("CA"); 
    object.setName("Cath"); 
    object.setSalary(300); 
    object.setProperties(new PropertiesMap()); 
    m.marshal(object, System.out); 

    } 
} 

@XmlRootElement(name="Employee") 
@XmlAccessorType(XmlAccessType.FIELD) 
class Employee { 
    private String code; 

    @XmlElement(name = "Name") 
    private String name; 

    private int salary; 

    @XmlElement(name = "Properties") 
    protected PropertiesMap params; 

    public String getCode() { 
    return code; 
    } 

    public void setCode(String code) { 
    this.code = code; 
    } 

    public PropertiesMap getProperties() { 
     return params; 
    } 

    public void setProperties(PropertiesMap value) { 
     this.params = value; 
    } 

    public String getName() { 
    return name; 
    } 

    public void setName(String name) { 
    this.name = name; 
    } 

    public int getSalary() { 
    return salary; 
    } 

    public void setSalary(int population) { 
    this.salary = population; 
    } 
} 


    @XmlRootElement(name="Properties") 
    class PropertiesMap<K,V> extends HashMap<K,V> 
    { 

    } 

上面的代碼生成以下XML與JDK 1.6JAXB不產生XML作爲每註解(JDK 1.7)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<Employee> 
    <code>CA</code> 
    <Name>Cath</Name> 
    <salary>300</salary> 
    <Properties/> 
</Employee> 

並且這在JDK 1.7

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<Employee> 
    <code>CA</code> 
    <Name>Cath</Name> 
    <salary>300</salary> 
    <params/> 
</Employee> 

爲什麼在Marshaller的行爲有所不同?

+1

我相信這不是因爲JDK版本,而是因爲不同的JAXB版本與JDK捆綁在一起。 JDK 1.7版本似乎對我來說是正確的,根元素名稱應該用作根元素或元素引用(我猜)。將「@ XmlElementRef」註釋添加到屬性時會發生什麼? – lexicore 2014-09-19 12:14:56

+0

@lexicore - 你是對的,這是由於JDK捆綁了不同的JAXB版本。實際的罪魁禍首是我的'XmlJavaTypeAdapter'。我已經創建了一個新的問題與此細節 - http://stackoverflow.com/questions/25942081/xmladapter-not-working-correctly-with-newer-version-of-jaxb – coderplus 2014-09-20 13:50:46

回答

1

您應該使用@XmlElementWrapper而不是@XmlElement作爲您的屬性地圖。

請參閱http://blog.bdoughan.com/2013/03/jaxb-and-javautilmap.html用例#2。

+0

這解決了它。我使用xjc來生成JAXB註釋類。所以我爲實際問題創建了一個新問題 - http://stackoverflow.com/questions/25942081/xmladapter-not-working-correctly-with-newer-version-of-jaxb – coderplus 2014-09-19 20:42:24