Скачиваний:
64
Добавлен:
15.06.2014
Размер:
3.63 Кб
Скачать
package by.WorkingWithFiles;

import java.io.FileWriter;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.Text;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import Products.Product;
import by.ProductData;

public class MainTableFileWriter
{
//constructor
public MainTableFileWriter()
{

}

//write information in xml file
public void writeToFile(ProductData productdata)
{
//create root element for our xml file
Element magazineElement = new Element("magazine");

//create new document and add into it root element
Document myDocument = new Document(magazineElement);// = new Document(productElement);

//get size of LinkedList where our
int counter = productdata.getSize();

//use for cicle for writing elements into xml file
for(int i = 0;i < counter;i++)
{
//get product object from ProductData
Product product = productdata.getElementWithPopping();

Element productElement = new Element("product");
magazineElement.addContent(productElement);

//adds name element into file
Element name = new Element("name");
name.addContent(product.getProductName());
productElement.addContent(name);

//adds price element into file
Element price = new Element("price");
price.addContent(product.getProductPrice());
productElement.addContent(price);

//adds arrival date into file
Element arrivaldate = new Element("arrivaldate");
arrivaldate.addContent(product.getProductArrivalDate());
productElement.addContent(arrivaldate);

//adds maker into file
Element maker = new Element("maker");
maker.addContent(product.getProductMaker());
productElement.addContent(maker);

//adds weight into file
Element weight = new Element("weight");
weight.addContent(product.getProductWeight());
productElement.addContent(weight);

//adds bruttoweight into file
Element bruttoweight = new Element("bruttoweight");
bruttoweight.addContent(product.getProductBruttoWeight());
productElement.addContent(bruttoweight);

//adds nettoweight into file
Element nettoweight = new Element("nettoweight");
nettoweight.addContent(product.getProductNettoWeight());
productElement.addContent(nettoweight);

//adds type into file
Element type = new Element("type");
type.addContent(product.getProductType());
productElement.addContent(type);

//adds restrictiononage into file
Element restrictiononage = new Element("restrictiononage");
restrictiononage.addContent(product.getProductRestrictionOnAge());
productElement.addContent(restrictiononage);

//adds department into file
Element department = new Element("department");
department.addContent(product.getProductDepartment());
productElement.addContent(department);

//adds department into file
Element shelflife = new Element("shelflife");
shelflife.addContent(product.getProductShelfLife());
productElement.addContent(shelflife);

//adds department into file
Element number = new Element("number");
number.addContent(product.getProductNumber());
productElement.addContent(number);
}

//try write into xml file our information
try {
FileWriter writer = new FileWriter("productdata.xml");
XMLOutputter outputter = new XMLOutputter();
outputter.setFormat(Format.getPrettyFormat());
outputter.output(myDocument, writer);
writer.close();
}
catch(java.io.IOException e)
{
e.printStackTrace();
}
}
}
Соседние файлы в папке WorkingWithFiles