2016-11-17 2 views
0

Ich habe ein einfaches Java-Programm, um ein Objekt in eine XML-Datei zu schreiben, mein Problem ist, dass egal wie ich es mache, kann ich nur 1 Objekt in der XML-Datei speichern. mein Code geht wie folgtSpeichern mehrerer Objekte in XML-Datei

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

     } 
     } 

aber selbst wenn ich Beispiel 2 Produkte, die ich in meiner XML-Datei nur ein Objekt zu erhalten (die richtig geschrieben ist)

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

Antwort

0

Sie können dieses Problem beheben, indem Sie eine Liste der Artikel zum Beispiel unter Verwendung von

Marshalling a list of products

Hier ist Product.java Refactoring:

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

Jetzt eine Produkte Einheit schaffen, haben ein Feld vom Typ Liste

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

Und schließlich eine 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(); 
     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

ahh danke mann! Ich habe es versucht und es hat funktioniert! es ist mir nie in den Sinn gekommen, es in eine Liste aufzunehmen! Ich bin neu in XML-Dateien im Allgemeinen, so bin ich jetzt ein bisschen verloren – Twhite1195

+0

Gern geschehen !!! Komm weiter auf S.O und du wirst gut ausgehen :) – alainlompo

+0

danke Mann! Dies ist wirklich der beste Ort, um Ratschläge zu finden und Antworten zu erhalten! – Twhite1195

0

Hmm, diese Art von ist wie fragen, ob es angeschlossen ist, aber Ihr Beispiel hat nur etwas. Sie brauchen eine Liste von irgendeiner Art. Fügen Sie die Produkte zur Liste hinzu und serialisieren Sie die Liste.

Schauen Sie sich auch XStream an, Sie können es bekommen, um die XML-Bits für Sie zu tun, und Sie müssen sich nicht mit den Javax-Sachen befassen. Es wird für Sie von und nach XML serialisiert.

+0

so, soll ich das Objekt in eine Liste hinzufügen und dann jedes Objekt in der Liste schreiben, dass die XML-Datei? – Twhite1195