public class test {
public void addNewBook(Element root,String name,String author,String date,String price)
{
try {
Element newbook=new Element("book");
newbook.addContent(new Element("name"));
newbook.addContent(new Element("author"));
newbook.addContent(new Element("publishDate"));
newbook.addContent(new Element("price"));
// List ls=newbook.getChildren();
Element name1=(Element)newbook.getChild("name");
// Element name1=e.getChild("name");
name1.setText(name);
Element author1=newbook.getChild("author");
author1.setText(author);
Element publishDate1=newbook.getChild("publishDate");
publishDate1.setText(date);
Element price1=newbook.getChild("price");
price1.setText(price);
root.addContent(newbook);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public void delBook(Element root,String name)
{
List booklist=root.getChildren();
System.out.println(booklist.size());
for(int i=0;i<booklist.size();i++)
{
System.out.println("boolist["+i+"]");
Element delbook=(Element)booklist.get(i);
String delbookname=null;
delbookname=delbook.getChild("name").getText().trim();
System.out.println("bookname="+delbookname);
if(delbookname.equals(name))
System.out.println(root.removeContent(delbook));
}
}
public ArrayList searchBook(Element root,String bookname)
{
List list=root.getChildren();
ArrayList booklist=new ArrayList();
Iterator it= list.iterator();
Element book=null;
while(it.hasNext())
{
book=(Element)it.next();
if(book.getChildText("name").equals(bookname))
booklist.add(book);
}
return booklist;
}
public test() {
}
public static void main(String[] args) {
test test1 = new test();
try {
SAXBuilder sb = new SAXBuilder();