2014-05-22 84 views
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>標籤,我不能給XMLRootElementobject

所以跟着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?

回答

0

這裏是一個例子,如果你不能Unmarshall,它會實際讀取XML並將其轉換爲JAXB特定的XML。

我將int更改爲Integers以允許空值並且能夠檢查是否遺留XML。

 

import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.DocumentBuilder; 
import org.w3c.dom.Document; 
import org.w3c.dom.NodeList; 
import org.w3c.dom.Node; 
import org.w3c.dom.Element; 
import java.io.IOException; 
import java.io.StringReader; 
import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBElement; 
import javax.xml.bind.Marshaller; 
import javax.xml.bind.Unmarshaller; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.parsers.ParserConfigurationException; 
import javax.xml.transform.stream.StreamSource; 
import org.xml.sax.EntityResolver; 
import org.xml.sax.InputSource; 
import org.xml.sax.SAXException; 

@XmlRootElement 
public class Bike { 

    String color; 
    Integer engineCapacity; 
    Integer 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 Integer getVin() { 
     return vin; 
    } 

    @XmlAttribute 
    public void setVin(int vin) { 
     this.vin = vin; 
    } 

    @Override 
    public String toString() { 
     return "Bike [color=" + color + ", engineCapacity=" + engineCapacity 
       + ", vin=" + vin + "]"; 
    } 


    public static void main(String argv[]) throws Exception { 
     StreamSource xml = new StreamSource("bike.xml");   
     JAXBContext jaxbContext = JAXBContext.newInstance(Bike.class); 
     Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
     JAXBElement je1 = jaxbUnmarshaller.unmarshal(xml, Bike.class); 
     Bike bike = je1.getValue(); 
     if (bike.color == null && bike.vin == null && bike.engineCapacity == null) { 
      System.out.println("LEGACY XML"); 
      Document doc = Bike.readXml(xml); 
      doc.getDocumentElement().normalize(); 
      NodeList nList = doc.getElementsByTagName("void"); 
      String color = null; 
      Integer vin = null; 
      Integer engineCapacity = null; 
      NodeList k; 
      for (int temp = 0; temp 0) { 
          color = k.item(0).getTextContent(); 
         } 
        } else { 
         k = eElement.getElementsByTagName("int"); 
         if (k.getLength() > 0) { 
          if (prop.equals("vin")) { 
           vin = Integer.valueOf(k.item(0).getTextContent()); 
          } else if (prop.equals("engineCapacity")) { 
           engineCapacity = Integer.valueOf(k.item(0).getTextContent()); 
          } 
         } 
        } 

       } 
      } 
      if (vin != null && engineCapacity != null && color != null) { 
       bike = new Bike(vin, engineCapacity, color); 

       Marshaller m = jaxbContext.createMarshaller(); 
       m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); 
       System.out.println("THIS WILL BE THE NEW XML CREATED"); 
       System.out.println("--------------"); 
       m.marshal(bike, System.out); 
       System.out.println("--------------"); 
       System.out.println(bike); 
      } 

     } else { 
      System.out.println(bike); 
     } 

    } 

    public static Document readXml(StreamSource is) throws SAXException, IOException, ParserConfigurationException { 

     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     dbf.setValidating(false); 
     dbf.setIgnoringComments(false); 
     dbf.setIgnoringElementContentWhitespace(true); 
     dbf.setNamespaceAware(true); 
     DocumentBuilder db = dbf.newDocumentBuilder(); 
     db.setEntityResolver(new NullResolver()); 
     InputSource is2 = new InputSource(); 
     is2.setSystemId(is.getSystemId()); 
     is2.setByteStream(is.getInputStream()); 
     is2.setCharacterStream(is.getReader()); 
     return db.parse(is2); 
    } 
} 

class NullResolver implements EntityResolver { 
    @Override 
    public InputSource resolveEntity(String publicId, String systemId) throws SAXException,IOException { 
     return new InputSource(new StringReader("")); 
    } 
} 
+0

感謝您的suggueion的gtgaxiola。但是會有一個性能問題嗎?由於我們多次處理相同的xml文檔。我有一個擁有10000s自行車數據的xml。怎麼樣java.beans.XMLDecoder – appu

+0

'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