同样用java追加xml有关问题。11
同样用java追加xml问题。急!!!!!!!!!!!!11
public class AddBook {
String id=null;
String title=null;
String author=null;
public AddBook(String id,String title,String author) {
this.id=id;
this.title=title;
this.author=author;
}
public void add(Document doc){//这是追加的方法
Element book=doc.createElement( "book ");
Element eid=doc.createElement( "id ");
Element etitle=doc.createElement( "title ");
Element eauthor=doc.createElement( "author ");
Text tid=doc.createTextNode(id);
Text ttitle=doc.createTextNode(title);
Text tauthor=doc.createTextNode(author);
Node nid=book.appendChild(eid).appendChild(tid);
Node ntitle=book.appendChild(etitle).appendChild(ttitle);
Node nauthro=book.appendChild(eauthor).appendChild(tauthor);
Node nbook=doc.getDocumentElement().appendChild(book);
}
public void ptintdoc(Document doc){
NodeList list=doc.getElementsByTagName( "* ");
Element e=null;
for (int i = 0; i < list.getLength(); i++) {
e=(Element) list.item(i);
System.out.println(e.getNodeName()+ ": "+e.getFirstChild().getNodeValue());
}
}
public static void main(String[] arg){
AddBook addbook=new AddBook(arg[0],arg[1],arg[2]);
DocumentBuilderFactory dbfactory=DocumentBuilderFactory.newInstance();
try {
DocumentBuilder dbbuilder = dbfactory.newDocumentBuilder();
Document doc=dbbuilder.parse(new File( "D:\\untitled1\\book.xml "));
addbook.add(doc);
public class AddBook {
String id=null;
String title=null;
String author=null;
public AddBook(String id,String title,String author) {
this.id=id;
this.title=title;
this.author=author;
}
public void add(Document doc){//这是追加的方法
Element book=doc.createElement( "book ");
Element eid=doc.createElement( "id ");
Element etitle=doc.createElement( "title ");
Element eauthor=doc.createElement( "author ");
Text tid=doc.createTextNode(id);
Text ttitle=doc.createTextNode(title);
Text tauthor=doc.createTextNode(author);
Node nid=book.appendChild(eid).appendChild(tid);
Node ntitle=book.appendChild(etitle).appendChild(ttitle);
Node nauthro=book.appendChild(eauthor).appendChild(tauthor);
Node nbook=doc.getDocumentElement().appendChild(book);
}
public void ptintdoc(Document doc){
NodeList list=doc.getElementsByTagName( "* ");
Element e=null;
for (int i = 0; i < list.getLength(); i++) {
e=(Element) list.item(i);
System.out.println(e.getNodeName()+ ": "+e.getFirstChild().getNodeValue());
}
}
public static void main(String[] arg){
AddBook addbook=new AddBook(arg[0],arg[1],arg[2]);
DocumentBuilderFactory dbfactory=DocumentBuilderFactory.newInstance();
try {
DocumentBuilder dbbuilder = dbfactory.newDocumentBuilder();
Document doc=dbbuilder.parse(new File( "D:\\untitled1\\book.xml "));
addbook.add(doc);