0
我有一個以XML格式生成數據的應用程序。最初,它產生的使用XML解碼器文件和XML示例是如下,爲xmldecoder文件創建JAXB綁定
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<java version="1.7.0_17" class="java.beans.XMLDecoder">
<object class="com.test.Bike">
<void property="color">
<string>military-green</string>
</void>
<void property="engineCapacity">
<int>150</int>
</void>
<void property="vin">
<int>215468</int>
</void>
</object>
</java>
後來XML編組技術改變爲JAXB。
現在我需要編寫一個應用程序,它使用JAXB綁定讀取新的xml和傳統xml文件。但我遇到了遺留xml綁定的問題。既然是有<java>
標籤,我不能給XMLRootElement
爲object
所以跟着JAXBContext
基於反編排如下
JAXBContext jaxbContext = JAXBContext.newInstance(Bike.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
StreamSource xml = new StreamSource("bike.xml");
JAXBElement<Bike> je1 = jaxbUnmarshaller.unmarshal(xml, Bike.class);
Bike bike = je1.getValue();
System.out.println(bike);
自行車Domain對象如下,
public class Bike {
String color;
int engineCapacity;
int vin;
public Bike() {
}
public Bike(int vin, int engineCapacity, String color) {
this.vin = vin;
this.engineCapacity = engineCapacity;
this.color = color;
}
public String getColor() {
return color;
}
@XmlElement
public void setColor(String color) {
this.color = color;
}
public int getEngineCapacity() {
return engineCapacity;
}
@XmlElement
public void setEngineCapacity(int engineCapacity) {
this.engineCapacity = engineCapacity;
}
public int getVin() {
return vin;
}
@XmlAttribute
public void setVin(int vin) {
this.vin = vin;
}
public String toString() {
return "Bike [color=" + color + ", engineCapacity=" + engineCapacity
+ ", vin=" + vin + "]";
}
}
但始終我得到輸出爲Bike [color=null, engineCapacity=0, vin=0]
任何人都可以幫助我如何使用JAXB有效地綁定這些xml?
感謝您的suggueion的gtgaxiola。但是會有一個性能問題嗎?由於我們多次處理相同的xml文檔。我有一個擁有10000s自行車數據的xml。怎麼樣java.beans.XMLDecoder – appu
'public static void main(String [] args)throws FileNotFoundException {File:} { BufferedInputStream bis = new BufferedInputStream(fis); XMLDecoder xmlDecoder = new XMLDecoder(bis); Bike mb =(Bike)xmlDecoder.readObject(); System.out.println(mb);}' – appu