从Java中的hashmap创建多个XML文件

时间:2016-07-14 16:56:40

标签: java xml jaxb

我正在尝试从HashMap创建XML文件。对于散列的每个键,我想要一个XML文件。键的值是Objects的ArrayList。我正在使用JAXB但是没有创建XML文件,因为输出不是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;
         }
    }

我该如何解决这个问题。提前谢谢。

1 个答案:

答案 0 :(得分: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);