简体   繁体   English

从Java中的哈希图创建多个XML文件

[英]Creating multiple XML files from a hashmap in Java

I'm trying to create a XML file from a HashMap. 我正在尝试从HashMap创建XML文件。 For each key of the hash i want an XML file. 对于哈希的每个键,我想要一个XML文件。 The value of the key is an ArrayList of Objects. 键的值是一个对象的ArrayList。 I am using JAXB but the XML files are not created, as the output is not XML valid. 我正在使用JAXB,但未创建XML文件,因为输出的XML无效。

The object class: 对象类:

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;
    }
}

The class where i try to create the XMLs: 我尝试创建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();
              }

        }
    }

The class for the root of the xml: 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;
         }
    }

How can i fix this. 我怎样才能解决这个问题。 Thanks in advance. 提前致谢。

You need to marshal an instance of ProdsList. 您需要封送ProdsList的实例。 Instead you are trying to marshall an ArrayList of Products. 相反,您尝试封送产品的ArrayList。

Change 更改

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

To

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM