在XML文件中存储多个对象

时间:2016-11-17 02:21:06

标签: java xml file object

我有一个简单的java程序将对象写入xml文件,我的问题是无论我怎么做,我都可以在xml文件中存储1个对象。 我的代码如下

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

        }
        }

但即使我实例化了2个产品,我的XML文件中只有一个对象(写得正确)

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

2 个答案:

答案 0 :(得分:0)

嗯,这有点像问是否插入,但你的例子只有东西。你需要一个某种列表。将产品添加到列表中,然后序列化列表。

另外看看XStream,你可以让它为你做xml位,你不必处理javax的东西。它将为您串行化为XML。

答案 1 :(得分:0)

您可以使用产品列表来解决此问题,例如

Marshalling a list of products

这是Product.java重构

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

现在创建一个具有List

类型字段的Products实体
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;
    }
}

最后演示:

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();
        prod2.setName("Bretzel");
        prod2.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();
    }
}