您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > BookXml增删改查
packagecom.pb.bdqn.BookDao;importjava.util.ArrayList;importjava.util.List;importorg.w3c.dom.Document;importorg.w3c.dom.Element;importorg.w3c.dom.NodeList;importcom.pb.bdqn.XmlUtil.XmlUtil;importcpm.pb.bdqn.bookEntity.Book;publicclassBookDao{/**查询所有书的信息*/publicListBookgetAll(){Documentdoc=XmlUtil.getDoc(xml/Students.xml);ArrayListBookbookList=newArrayListBook();if(doc!=null){//4.读取书的信息NodeListlist=doc.getElementsByTagName(book);Bookbook;for(inti=0;ilist.getLength();i++){ElementbookElement=(Element)list.item(i);Stringid=bookElement.getAttribute(id);Stringname=bookElement.getElementsByTagName(name).item(0).getFirstChild().getNodeValue();Stringauthor=bookElement.getElementsByTagName(author).item(0).getFirstChild().getNodeValue();Stringpubdate=bookElement.getElementsByTagName(pubdate).item(0).getFirstChild().getNodeValue();Stringpress=bookElement.getElementsByTagName(press).item(0).getFirstChild().getNodeValue();book=newBook(id,name,author,pubdate,press);bookList.add(book);}returnbookList;}else{returnnull;}}/**通过编号查询书的信息*/publicListBookgetbookById(Stringid){Bookbook=null;Documentdoc=XmlUtil.getDoc(xml/Students.xml);ArrayListBookbookList=newArrayListBook();if(doc!=null){//4.读取书的信息NodeListlist=doc.getElementsByTagName(book);for(inti=0;ilist.getLength();i++){ElementbookElement=(Element)list.item(i);StringbookId=bookElement.getAttribute(id);if(bookId.equals(id)){book=newBook();Stringname=bookElement.getElementsByTagName(name).item(0).getFirstChild().getNodeValue();Stringauthor=bookElement.getElementsByTagName(author).item(0).getFirstChild().getNodeValue();Stringpubdate=bookElement.getElementsByTagName(pubdate).item(0).getFirstChild().getNodeValue();Stringpress=bookElement.getElementsByTagName(press).item(0).getFirstChild().getNodeValue();book.setId(bookId);book.setName(name);book.setAuthor(author);book.setPubdate(pubdate);book.setPress(press);bookList.add(book);break;}}returnbookList;}else{returnnull;}}/**增加图书信息*/publicbooleanaddBk(Bookbook){Documentdoc=XmlUtil.getDoc(xml/Students.xml);Elementroot=doc.getDocumentElement();//4.创建新的节点//创建一个book新节点Elementbk=doc.createElement(book);bk.setAttribute(id,b005);root.appendChild(bk);//创建name节点Elementname=doc.createElement(name);name.setTextContent(book.getName());//创建author节点Elementauthor=doc.createElement(author);author.setTextContent(book.getAuthor());//创建pubdate节点Elementpubdate=doc.createElement(pubdate);pubdate.setTextContent(book.getPubdate());//创建press节点Elementpress=doc.createElement(press);press.setTextContent(book.getPress());bk.appendChild(name);bk.appendChild(author);bk.appendChild(pubdate);bk.appendChild(press);returnXmlUtil.save(doc,src/Book.xml);}/**修改图书*/publicbooleanmodifyBk(Bookbk){Documentdoc=XmlUtil.getDoc(xml/Students.xml);Elementroot=doc.getDocumentElement();NodeListlist=root.getElementsByTagName(book);for(inti=0;ilist.getLength();i++){Elementbook=(Element)list.item(i);Stringid=book.getAttribute(id);if(id.equals(bk.getId())){book.getElementsByTagName(name).item(0).setTextContent(bk.getName());book.getElementsByTagName(author).item(0).setTextContent(bk.getAuthor());book.getElementsByTagName(pubdate).item(0).setTextContent(bk.getPubdate());book.getElementsByTagName(press).item(0).setTextContent(bk.getPress());break;}}returnXmlUtil.save(doc,src/Book.xml);}/**删除图书*/publicbooleandeleteBk(Stringid){Documentdoc=XmlUtil.getDoc(xml/Students.xml);Elementroot=doc.getDocumentElement();NodeListlist=root.getElementsByTagName(book);for(inti=0;ilist.getLength();i++){Elementbook=(Element)list.item(i);StringbkId=book.getAttribute(id);if(id.equals(bkId)){book.getParentNode().removeChild(book);break;}}returnXmlUtil.save(doc,src/Book.xml);}}packagecom.pb.bdqn.bookBiz;importjava.util.InputMismatchException;importjava.util.List;importjava.util.Scanner;importcom.pb.bdqn.BookDao.BookDao;importcpm.pb.bdqn.bookEntity.Book;publicclassbookBiz{Bookbook=newBook();BookDaobookdao=newBookDao();Scannerinput=newScanner(System.in);Stringid=null;Stringname=null;Stringauthor=null;Stringpubdate=null;Stringpress=null;intoption=0;//开始菜单publicvoidstartMenu(){System.out.println(欢迎使用图书管理系统!);System.out.println(1.查询所有图书信息\n2.通过编号进行查询);System.out.println(3.增加节点);System.out.println(4.修改节点);System.out.println(5.删除节点);System.out.println(请输入1/2/3/4/5:);do{try{option=input.nextInt();if(option=1&&option=5){break;}else{System.out.println(输入错误,请重新输入:);}}catch(InputMismatchExceptionime){System.out.println(输入错误,请重新输入:);input.next();}}while(true);switch(option){case1://打印输出所有图书信息printAll();break;case2://利用编号查询图书信息printById();break;case3://添加图书信息add();break;case4://修改图书信息modify();break;case5://删除图书信息delete();break;default:break;}}//case1:遍历所有图书信息publicvoidprintAll(){ListBooklist=bookdao.getAll();if(list!=null){System.out.println(编号\t书名\t作者\t出版日期\t出版社);for(Bookbook:list){System.out.println(book.getId()+\t+book.getName()+\t+book.getAuthor()+\t+book.getPubdate()+\t+book.getPress());}}else{System.out.println(读取XML异常);}startMenu();}//case2:通过编号查询图书的信息publicvoidprintById(){System.out.println(请输入图书编号:
本文标题:BookXml增删改查
链接地址:https://www.777doc.com/doc-2900831 .html