2016-07-14 59 views
0

我想從一個HashMap創建一個XML文件。對於每個哈希鍵我想要一個XML文件。密鑰的值是對象的ArrayList。我正在使用JAXB,但不會創建XML文件,因爲輸出不是XML有效的。從Java中的hashmap創建多個XML文件

對象類:

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

@XmlRootElement(name = "Product") 
public class Product implements Comparable<Product>{ 
    String ID,description, gtin; 
    double price; 
    String date; 
    Product() 
    { 

    } 
    public String toString() 
    { 
     return ID+" "+description+" "+gtin+" "+price+" "+date; 
    } 
    public String getID() { 
     return ID; 
    } 
    @XmlElement 
    public void setID(String ID) { 
     this.ID = ID; 
    } 

    public String getDescription() { 
     return description; 
    } 
    @XmlElement 
    public void setDescription(String description) { 
     this.description = description; 
    } 

    public String getGtin() { 
     return gtin; 
    } 
    @XmlElement 
    public void setGtin(String gtin) { 
     this.gtin = gtin; 
    } 

    public double getPrice() { 
     return price; 
    } 
    @XmlElement 
    public void setPrice(Double price) { 
     this.price = price; 
    } 
} 

在那裏我嘗試創建個XML類:

import java.io.File; 
    import java.util.ArrayList; 
    import java.util.HashMap; 
    import java.util.Iterator; 
    import java.util.Set; 

    import javax.xml.bind.JAXBContext; 
    import javax.xml.bind.JAXBException; 
    import javax.xml.bind.Marshaller; 

    public class CreateXML { 
     static void create(HashMap<String, ArrayList<Product> > map) { 

      try { 

       JAXBContext jaxbContext = JAXBContext.newInstance(ProdsList.class); 
       Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); 

       jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
       Set setOfKeys = map.keySet(); 
       Iterator iterator = setOfKeys.iterator(); 
      while (iterator.hasNext()) { 
      String keys = (String) iterator.next(); 
      String filename= "C:\\Users\\As\\Desktop\\Sups\\"+keys+22+".xml"; 
      File file = new File(filename); 
       ArrayList<Product> value = map.get(keys); 
      jaxbMarshaller.marshal(value, file); 
      jaxbMarshaller.marshal(value, System.out); 
      } 
       } catch (JAXBException e) { 
      e.printStackTrace(); 
       } 

     } 
    } 

爲XML的根類:

import java.util.*; 

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


    //@XmlAccessorType(XmlAccessType.FIELD) 
    @XmlRootElement(name="Products") 
    //@XmlSeeAlso({ArrayList.class}) 
    class ProdsList { 

     @XmlElement(name="Product") 
     ArrayList<Product> prods; 

     public ProdsList(){ 
       prods=new ArrayList<Product>(); 
      } 
     public ArrayList<Product> getProducts() { 
      return prods; 
     } 

     public void setProducts(ArrayList<Product> prods) { 
      this.prods = prods; 
     } 
    } 

哪有我解決這個問題。提前致謝。

+0

「輸出不是XML有效的。」你怎麼知道? – VGR

回答

1

你需要編組一個ProdsList的實例。相反,您正試圖編寫產品ArrayList。

變化

jaxbMarshaller.marshal(value, file); 
jaxbMarshaller.marshal(value, System.out); 

jaxbMarshaller.marshal(new ProdsList(value), file); 
jaxbMarshaller.marshal(new ProdsList(value), System.out);