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

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import Products.Product;
import by.ProductData;

public class MainTableFileReader
{
//constructor
public MainTableFileReader()
{

}

//read information from xml file
public void readFromFile(ProductData productdata)
{
//create SAXBuilder object for building Document using a SAX parser
SAXBuilder builder = new SAXBuilder();

//open file using constructor from java.io.File
File xmlFile = new File("productdata.xml");

try
{
//create Document object and bind it with our xml file
//where we store information about products
Document document = (Document)builder.build(xmlFile);

//create Element object and get for it root element from our document
Element rootNode = document.getRootElement();

//return the list of all the children elements
//nested directly
List list = rootNode.getChildren("product");

//in cicle we parse and enter information from xml file
//in ProductData object
for(int i = 0;i < list.size();i++)
{
//create Element object ang get for it element from xml file
//numbered i
Element node = (Element)list.get(i);

//create Product object for getting data from element
Product product = new Product();

//get info and set it into Product object
product.setProductName(node.getChildText("name"));
product.setProductPrice(node.getChildText("price"));
product.setProductArrivalDate(node.getChildText("arrivaldate"));
product.setProductMaker(node.getChildText("maker"));
product.setProductWeight(node.getChildText("weight"));
product.setProductBruttoWeight(node.getChildText("bruttoweight"));
product.setProductNettoWeight("nettoweight");
product.setProductType(node.getChildText("type"));
product.setProductRestrictionOnAge(node.getChildText("restrictiononage"));
product.setProductDepartment(node.getChildText("department"));
product.setProductShelfLife(node.getChildText("shelflife"));
product.setProductNumber(node.getChildText("number"));

//write Product object into ProductData object
productdata.addElement(product);
}
}

catch(IOException io)
{
System.out.println(io.getMessage());
}
catch(JDOMException jdomex)
{
//try write to log file our problems
System.out.println(jdomex.getMessage());
}
}
}
Соседние файлы в папке WorkingWithFiles