import java.io.File; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class Book { public static final String CLASS_NAME = Book.class.getName(); public static final String TAG_NAME_ASIN = "Asin"; public static final String TAG_NAME_TITLE = "Title"; public static final String TAG_NAME_AUTHOR = "Author"; public static final String TAG_NAME_PRICE = "Price"; public static final String TAG_NAME_PAGE = "Page"; private String asin = null; private String title = null; private String author = null; private Integer price = null; private List pageList = null; // default public Book() { this.pageList = new ArrayList(); } // field parameters public Book(String asin, String title, String author, Integer price, List pageList) { this.asin = asin; this.title = title; this.author = author; this.price = new Integer(price); this.pageList = new ArrayList(pageList); } // element public Book(Element element) throws Exception { // asin NodeList nodeListAsin = element.getElementsByTagName(TAG_NAME_ASIN); for (int i = 0; i < nodeListAsin.getLength(); i++) { Element elementAsin = (Element)nodeListAsin.item(i); this.asin = elementAsin.getFirstChild().getNodeValue(); } // title NodeList nodeListTitle = element.getElementsByTagName(TAG_NAME_TITLE); for (int i = 0; i < nodeListTitle.getLength(); i++) { Element elementTitle = (Element)nodeListTitle.item(i); this.title = elementTitle.getFirstChild().getNodeValue(); } // author NodeList nodeListAuthor = element.getElementsByTagName(TAG_NAME_AUTHOR); for (int i = 0; i < nodeListAuthor.getLength(); i++) { Element elementAuthor = (Element)nodeListAuthor.item(i); this.author = elementAuthor.getFirstChild().getNodeValue(); } // price NodeList nodeListPrice = element.getElementsByTagName(TAG_NAME_PRICE); for (int i = 0; i < nodeListPrice.getLength(); i++) { Element elementPrice = (Element)nodeListPrice.item(i); try { if (!StringUtil.isEmpty(elementPrice.getFirstChild().getNodeValue())) { this.price = new Integer(elementPrice.getFirstChild().getNodeValue()); } } catch (NumberFormatException e) { throw new Exception("failed to parse ."); } } // pageList NodeList nodeListPage = element.getElementsByTagName(TAG_NAME_PAGE); this.pageList = new ArrayList(); for (int i = 0; i < nodeListPage.getLength(); i++) { this.pageList.add(new Page((Element)nodeListPage.item(i))); } } // xml file name public Book(String xmlName) throws Exception { try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(new File(xmlName)); Element root = document.getDocumentElement(); // root name check if (!CLASS_NAME.equals(root.getTagName())) { throw new Exception("root name error."); } // asin NodeList nodeListAsin = root.getElementsByTagName(TAG_NAME_ASIN); for (int i = 0; i < nodeListAsin.getLength(); i++) { Element elementAsin = (Element)nodeListAsin.item(i); this.asin = elementAsin.getFirstChild().getNodeValue(); } // title NodeList nodeListTitle = root.getElementsByTagName(TAG_NAME_TITLE); for (int i = 0; i < nodeListTitle.getLength(); i++) { Element elementTitle = (Element)nodeListTitle.item(i); this.title = elementTitle.getFirstChild().getNodeValue(); } // author NodeList nodeListAuthor = root.getElementsByTagName(TAG_NAME_AUTHOR); for (int i = 0; i < nodeListAuthor.getLength(); i++) { Element elementAuthor = (Element)nodeListAuthor.item(i); this.author = elementAuthor.getFirstChild().getNodeValue(); } // price NodeList nodeListPrice = root.getElementsByTagName(TAG_NAME_PRICE); for (int i = 0; i < nodeListPrice.getLength(); i++) { Element elementPrice = (Element)nodeListPrice.item(i); try { if (!StringUtil.isEmpty(elementPrice.getFirstChild().getNodeValue())) { this.price = new Integer(elementPrice.getFirstChild().getNodeValue()); } } catch (NumberFormatException e) { throw new Exception("failed to parse ."); } } // pageList NodeList nodeListPage = root.getElementsByTagName(TAG_NAME_PAGE); this.pageList = new ArrayList(); for (int i = 0; i < nodeListPage.getLength(); i++) { this.pageList.add(new Page((Element)nodeListPage.item(i))); } } catch (Exception e) { throw e; } } public void setAsin(String asin) { this.asin = asin; } public void setTitle(String title) { this.title = title; } public void setAuthor(String author) { this.author = author; } public void setPrice(Integer price) { this.price = new Integer(price); } public void setPageList(List pageList) { this.pageList = new ArrayList(pageList); } public String getAsin() { return this.asin; } public String getTitle() { return this.title; } public String getAuthor() { return this.author; } public Integer getPrice() { return this.price; } public List getPageList() { return this.pageList; } public String toString() { return "[asin=" + this.asin + ", title=" + this.title + ", author=" + this.author + ", price=" + this.price + ", pageList=" + this.pageList + "]"; } }