Object books = session.getAttribute("BOOKLIST"); //retrieve "BOOKLIST" from session object
//"BOOKLIST" is an array of BookBean
if (books == null) { //if no booklist stored in seession object
BookBean [] newbooklist = new BookBean [10]; //create a new booklist with 10 elements
newbooklist[0] = newBook; //add first book to the lis
session.setAttribute("BOOKLIST", newbooklist); //insert the booklist into session object
return true;
}
else {
BookBean [] booklist = (BookBean []) books;
int newbookindex = 10; //if newbookindex = 10 : represents book list is full
for (int i=0; i<4; i++) {
if (booklist[i] == null) {
newbookindex = i;
break;
}
}
if (newbookindex == 10) { //booklist is full
return false;
}
else { //booklist is NOT full
booklist[newbookindex] = newBook;
session.setAttribute("BOOKLIST", booklist);
Document document = builder.newDocument();
Element root = document.createElement("booklist");
document.appendChild(root);
HttpSession session= request.getSession(false); //assume there is always a session exist
Object books = session.getAttribute("BOOKLIST"); //assume there is always an attribute contain a list of book
//you should do proper checking here in your project.
BookBean [] booklist = (BookBean []) books; //assume the list contains at most 10 books
int bookcount = 0;
for (int i=0; i<booklist.length; i++) {
if (booklist[i] != null)
bookcount++;
else
continue;
}
//creating the domcument tree that store the records in "BOOKLIST"
for (int j=0; j<bookcount; j++) {
Element bookElement = document.createElement("book");
Element bookTitleElement = document.createElement("title");
Text textnode1 = document.createTextNode(booklist[j].getTitle());
bookTitleElement.appendChild(textnode1);
bookElement.appendChild(bookTitleElement);
Element bookAuthorElement = document.createElement("author");
Text textnode2 = document.createTextNode(booklist[j].getAuthor());
bookAuthorElement.appendChild(textnode2);
bookElement.appendChild(bookAuthorElement);
Element bookISBNElement = document.createElement("isbn");
Text textnode3 = document.createTextNode(booklist[j].getIsbn());
bookISBNElement.appendChild(textnode3);
bookElement.appendChild(bookISBNElement);
Element bookPublisherElement = document.createElement("publisher");
Element publisherNameElement = document.createElement("name");
Text textnode4 = document.createTextNode(booklist[j].getPublisher());
publisherNameElement.appendChild(textnode4);
bookPublisherElement.appendChild(publisherNameElement);
Element publisherCountryElement = document.createElement("country");
Text textnode5 = document.createTextNode(booklist[j].getPublisherCountry());
publisherCountryElement.appendChild(textnode5);
bookPublisherElement.appendChild(publisherCountryElement);
bookElement.appendChild(bookPublisherElement);
public void generateXMLDocument (Document document) throws IOException {
File newXMLFile = new File("..//webapps//book//xml//booklist.xml");
FileWriter xmlwriter = new FileWriter(newXMLFile);
StringWriter stringOut = new StringWriter(); //Writer will be a String
OutputFormat xmlformat = new OutputFormat(document);
xmlformat.setIndenting(true);
XMLSerializer serializer1 = new XMLSerializer(xmlwriter, xmlformat);
XMLSerializer serializer2 = new XMLSerializer(stringOut, xmlformat);