Andy
Andy

Reputation: 155

Creating multiple XML files from a hashmap in Java

I'm trying to create a XML file from a HashMap. For each key of the hash i want an XML file. The value of the key is an ArrayList of Objects. I am using JAXB but the XML files are not created, as the output is not XML valid.

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:

 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:

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.

Upvotes: 0

Views: 902

Answers (1)

garnulf
garnulf

Reputation: 366

You need to marshal an instance of ProdsList. Instead you are trying to marshall an ArrayList of Products.

Change

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

To

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

Upvotes: 1

Related Questions