2016-11-17 83 views
0

我有一个简单的java程序来将一个对象写入一个xml文件,我的问题是不管我如何做,我只能将一个对象存储在xml文件中。 我的代码去如下在XML文件中存储多个对象

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> 

回答

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

现在建立,将有一个产品的实体字段列表

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

啊谢谢你!我试了一下,它的工作!我从来没有想过将它列入名单!我是一般的XML文件新手,所以我现在有点迷路了 – Twhite1195

+0

不客气!继续来S.O,你会变好:) – alainlompo

+0

谢谢你!这真的是寻找建议的最佳地点,并获得答案! – Twhite1195

0

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

另外看看XStream,你可以得到它为你做xml位,你将不必处理javax的东西。它会为您序列化XML和XML。

+0

所以,我要的对象添加到列表,然后在列表中的每个对象编写XML文件? – Twhite1195