Twhite1195 Twhite1195 - 22 days ago 6
Java Question

storing multiple objects in XML file

I have a simple java program to write an object to a xml file, my problem is that no matter how I do it, I can just store 1 object in the xml file.
my code goes as follows

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

@XmlRootElement
public class Product {

String Name;
int Price;

@XmlElement
public void setName(String Name) {
this.Name = Name;
}

@XmlElement
public void setPrice(int price) {
this.price = price;
}
}
import xml.Product;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class XML {


public static void main(String[] args) {

Product product=new Product();
product.setName("Hamburger");
product.setPrice(10);

try{
//File file = new File("C:\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Product.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

jaxbMarshaller.marshal(product, file);
jaxbMarshaller.marshal(product, System.out);
}catch(JAXBException e){
e.printStackTrace();
}

}
}


but even if I instance 2 products, I get just one object in my XML file(which is written correctly)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Product>
<Name>Hamburger</Name>
<price>10</price>
</Product>

Answer

You can fix this by using a List of Product for example

marshalling a list of products

Here is Product.java refactored:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "product")
@XmlAccessorType (XmlAccessType.FIELD)
public class Product {
    private String Name;
    private int price;

    public String getName() {
        return Name;
    }

    public int getPrice() {
        return price;
    }

    public void setName(String Name) {
        this.Name = Name;
    }

    public void setPrice(int price) {
        this.price = price;
    }
}

Now create a Products entity that will have a field of type List

import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "products")
@XmlAccessorType (XmlAccessType.FIELD)
public class Products {
    @XmlElement(name = "product")
    private List<Product> products = null;

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }
}

And finally a demo:

import java.io.File;
import java.util.ArrayList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class ProductsDump {
    static Products products = new Products();
    static
    {
        products.setProducts(new ArrayList<>()); 
        Product prod1 = new Product();
        prod1.setName("Hamburger");
        prod1.setPrice(10);
        Product prod2 = new Product();
        prod1.setName("Bretzel");
        prod1.setPrice(5);
        products.getProducts().add(prod1);
        products.getProducts().add(prod2);
    }
    private static void marshalingExample() throws JAXBException
    {
        JAXBContext jaxbContext = JAXBContext.newInstance(Products.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        //Marshal the products list in console
        jaxbMarshaller.marshal(products, System.out);

        //Marshal the products list in file
        jaxbMarshaller.marshal(products, new File("c:/products.xml"));
    }

    public static void main(String[] args) throws Exception {
        marshalingExample();
    }
}