主题:XML文件的插入
下面的代码插入方法出错了 不知道是什么问题
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
// 写入xml文件
public class WriteXML {
static int i=0;
Element people0;
Element root, name, nickname, pass,love,city,six,jieshao;
public void wirteXml(){
root = new Element("people-info"); //生成根元素:people-info
i++;
people0 = new Element("people"+i);
name = new Element("name");
nickname = new Element("nickname");
pass = new Element("pass");
love=new Element("love");
city=new Element("city");
six=new Element("six");
jieshao=new Element("jieshao");
Document doc = new Document(root); //将根元素植
name.setText("11111111");
// name.setText(people.name);
// nickname.setText(people.nickname);
// pass.setText(people.pass);
// love.setText(people.love);
// city.setText(people.city);
// six.setText(people.six);
// jieshao.setText(people.jieshao);
people0.addContent(name);
people0.addContent(nickname);
people0.addContent(pass);
people0.addContent(love);
people0.addContent(city);
people0.addContent(six);
people0.addContent(jieshao);
root.addContent(people0);
Format format = Format.getCompactFormat();
format.setEncoding("gb2312"); //设置xml文件的字符为gb2312
format.setIndent(" "); //设置xml文件的缩进为4个空格
XMLOutputter XMLOut = new XMLOutputter(format);//在元素后换行,每一层元素缩排四格
try {
XMLOut.output(doc, new FileOutputStream("E:\\book\\zhuce\\people-info.xml"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void insert(){
i++;
SAXBuilder builder = new SAXBuilder();
Document read_doc=null;
try {
read_doc = builder.build("E:\\book\\zhuce\\people-info.xml");
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Element stu = read_doc.getRootElement();
people0 = new Element("people"+i);
name = new Element("name");
nickname = new Element("nickname");
pass = new Element("pass");
love=new Element("love");
city=new Element("city");
six=new Element("six");
jieshao=new Element("jieshao");
//Document doc = new Document(root); //将根元素植
name.setText("22222222222");
// name.setText(people.name);
// nickname.setText(people.nickname);
// pass.setText(people.pass);
// love.setText(people.love);
// city.setText(people.city);
// six.setText(people.six);
// jieshao.setText(people.jieshao);
people0.addContent(name);
people0.addContent(nickname);
people0.addContent(pass);
people0.addContent(love);
people0.addContent(city);
people0.addContent(six);
people0.addContent(jieshao);
stu.addContent(people0);
Document doc = new Document(stu);
Format format = Format.getCompactFormat();
format.setEncoding("gb2312"); //设置xml文件的字符为gb2312
format.setIndent(" "); //设置xml文件的缩进为4个空格
XMLOutputter XMLOut = new XMLOutputter(format);//在元素后换行,每一层元素缩排四格
try {
XMLOut.output(doc, new FileOutputStream("E:\\book\\zhuce\\people-info.xml"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
WriteXML w=new WriteXML();
w.wirteXml();
w.insert();
System.out.println("aaaaaaaa");
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
// 写入xml文件
public class WriteXML {
static int i=0;
Element people0;
Element root, name, nickname, pass,love,city,six,jieshao;
public void wirteXml(){
root = new Element("people-info"); //生成根元素:people-info
i++;
people0 = new Element("people"+i);
name = new Element("name");
nickname = new Element("nickname");
pass = new Element("pass");
love=new Element("love");
city=new Element("city");
six=new Element("six");
jieshao=new Element("jieshao");
Document doc = new Document(root); //将根元素植
name.setText("11111111");
// name.setText(people.name);
// nickname.setText(people.nickname);
// pass.setText(people.pass);
// love.setText(people.love);
// city.setText(people.city);
// six.setText(people.six);
// jieshao.setText(people.jieshao);
people0.addContent(name);
people0.addContent(nickname);
people0.addContent(pass);
people0.addContent(love);
people0.addContent(city);
people0.addContent(six);
people0.addContent(jieshao);
root.addContent(people0);
Format format = Format.getCompactFormat();
format.setEncoding("gb2312"); //设置xml文件的字符为gb2312
format.setIndent(" "); //设置xml文件的缩进为4个空格
XMLOutputter XMLOut = new XMLOutputter(format);//在元素后换行,每一层元素缩排四格
try {
XMLOut.output(doc, new FileOutputStream("E:\\book\\zhuce\\people-info.xml"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void insert(){
i++;
SAXBuilder builder = new SAXBuilder();
Document read_doc=null;
try {
read_doc = builder.build("E:\\book\\zhuce\\people-info.xml");
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Element stu = read_doc.getRootElement();
people0 = new Element("people"+i);
name = new Element("name");
nickname = new Element("nickname");
pass = new Element("pass");
love=new Element("love");
city=new Element("city");
six=new Element("six");
jieshao=new Element("jieshao");
//Document doc = new Document(root); //将根元素植
name.setText("22222222222");
// name.setText(people.name);
// nickname.setText(people.nickname);
// pass.setText(people.pass);
// love.setText(people.love);
// city.setText(people.city);
// six.setText(people.six);
// jieshao.setText(people.jieshao);
people0.addContent(name);
people0.addContent(nickname);
people0.addContent(pass);
people0.addContent(love);
people0.addContent(city);
people0.addContent(six);
people0.addContent(jieshao);
stu.addContent(people0);
Document doc = new Document(stu);
Format format = Format.getCompactFormat();
format.setEncoding("gb2312"); //设置xml文件的字符为gb2312
format.setIndent(" "); //设置xml文件的缩进为4个空格
XMLOutputter XMLOut = new XMLOutputter(format);//在元素后换行,每一层元素缩排四格
try {
XMLOut.output(doc, new FileOutputStream("E:\\book\\zhuce\\people-info.xml"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
WriteXML w=new WriteXML();
w.wirteXml();
w.insert();
System.out.println("aaaaaaaa");
}
}