Скачиваний:
62
Добавлен:
15.06.2014
Размер:
4.16 Кб
Скачать
package workingwithfiles;

import java.io.FileWriter;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import products.BaseProduct;
import datacontainers.BaseProductContainer;

public class BaseProductFileWriter {
//baseconstructor
public BaseProductFileWriter() {

}

//write information in xml file
public void writeToFile(BaseProductContainer baseproductcontainer,String filename)
{
//check for empty input container
if(baseproductcontainer.getAllListOfProducts().size() == 0) {
try {
FileWriter writer = new FileWriter(filename);
writer.close();
}
catch(java.io.IOException exception) {
exception.printStackTrace();
}
return;
}

//create root element for our xml file
Element baseproductcontainerElement = new Element("baseproductcontainer");

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

//get size of LinkedList where our information is situated
int counter = baseproductcontainer.getAllListOfProducts().size();

//use for cicle for writing elements into xml file
for(int i = 0;i < counter;i++)
{
//get product product object
BaseProduct baseproduct = new BaseProduct(baseproductcontainer.getElementWithPopping());

Element baseproductElement = new Element("baseproduct");
baseproductcontainerElement.addContent(baseproductElement);

//add elements into baseproductElement
Element name = new Element("name");
name.addContent(baseproduct.getBaseProductName());
baseproductElement.addContent(name);

Element arrivaldate = new Element("arrivaldate");
arrivaldate.addContent(baseproduct.getBaseProductArrivalDate());
baseproductElement.addContent(arrivaldate);

Element maker = new Element("maker");
maker.addContent(baseproduct.getBaseProductMaker());
baseproductElement.addContent(maker);

Element price = new Element("price");
price.addContent(baseproduct.getBaseProductPrice());
baseproductElement.addContent(price);

Element type = new Element("type");
type.addContent(baseproduct.getBaseProductType());
baseproductElement.addContent(type);

Element shelflife = new Element("shelflife");
shelflife.addContent(baseproduct.getBaseProductShelfLife());
baseproductElement.addContent(shelflife);

Element weight = new Element("weight");
weight.addContent(baseproduct.getBaseProductWeight());
baseproductElement.addContent(weight);

Element number = new Element("number");
number.addContent(baseproduct.getBaseProductNumber());
baseproductElement.addContent(number);

Element country = new Element("country");
country.addContent(baseproduct.getBaseProductCountry());
baseproductElement.addContent(country);

Element makeraddress = new Element("makeraddress");
makeraddress.addContent(baseproduct.getBaseProductMakerAddress());
baseproductElement.addContent(makeraddress);

Element makersite = new Element("makersite");
makersite.addContent(baseproduct.getBaseProductMakerSite());
baseproductElement.addContent(makersite);

Element makermail = new Element("makermail");
makermail.addContent(baseproduct.getBaseProductMakerMail());
baseproductElement.addContent(makermail);

Element makerphone = new Element("makerphone");
makerphone.addContent(baseproduct.getBaseProductMakerPhone());
baseproductElement.addContent(makerphone);

Element department = new Element("department");
department.addContent(baseproduct.getBaseProductDepartment());
baseproductElement.addContent(department);
}

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