jar包
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.6</version>
</dependency>
1.创建Xml
/**
* 创建XMl文件
*/
public static void createXml(){
try{
//1.生成一个根节点
Element root = new Element("xml");
//2.创建一个document对象
Document document = new Document(root);
//3.创建根里的节点内容
for (int i = 1;i <= 5;i++){
//创建一个person节点
Element person = new Element("person");
//person节点添加节点属性
person.setAttribute("id",String.valueOf(i));
Element id = new Element("id");//创建id节点
//这里说明一下,new CDATA()生成xml后节点是:<id><![CDATA[value]]></id>
id.addContent(new CDATA(String.valueOf(i)));
//如果使用的是setText(),生成xml则是正常的value <id>value</id>
// id.setText("1");
person.addContent(id);
Element name = new Element("name");//创建name节点
// name.setText("小明");//学生姓名
name.addContent(new CDATA("小明"));
person.addContent(name);
Element card = new Element("card");//创建card节点
// card.setText("1001");//card存为学号
card.addContent(new CDATA("1001"));
person.addContent(card);
root.addContent(person);
}
Format format = Format.getCompactFormat();
format.setIndent(" ");//设置换行Tab或空格
format.setEncoding("UTF-8");//设置字符
//4.创建Xml输出对象XMLOutputter
XMLOutputter outputter = new XMLOutputter(format);
File file = new File("person.xml");
//5.用outputter 将document 转成xml文件
outputter.output(document,new FileOutputStream(file));
System.out.println("生成XML文件成功!");
}catch (Exception e){
e.printStackTrace();
}
}
说明:
这里说明一下,new CDATA()生成xml后节点是:<![CDATA[value]]>
id.addContent(new CDATA(String.valueOf(i)));
如果使用的是setText(),生成xml则是正常的value
id.setText(“1”);
2.解析Xml
/**
* 解析Xml文件
*/
public static void analyzeXml(){
try{
File file = new File("person.xml");
//创建一个解析器
SAXBuilder builder = new SAXBuilder();
//创建一个输入流
InputStream in = new FileInputStream(file);
//将流加载到解析器中,获取document对象
Document document = builder.build(in);
//获取xml根节点
Element root = document.getRootElement();
//getChild 是获取root节点下第一个person节点
// Element element = root.getChild("person");
//getChildren 是获取root节点下所有person节点
List<Element> personList = root.getChildren("person");
//循环所有的person节点
for (Element element : personList) {
System.out.println("element = " + element.getAttributeValue("id"));
//是获取person节点下所有节点
List<Element> els = element.getChildren();
for (Element el : els) {
System.out.println(el.getName() + " : " + el.getText());
}
}
}catch (Exception e){
e.printStackTrace();
}
}
说明:
getChild 是获取root节点下第一个person节点
Element element = root.getChild(“person”);
getChildren 是获取root节点下所有person节点
List personList = root.getChildren(“person”);
3.追加Xml节点
/**
* 追加节点
*/
public static void addToXml(){
try{
File file = new File("person.xml");
//创建一个解析器
SAXBuilder builder = new SAXBuilder();
//创建输入流
InputStream in = new FileInputStream(file);
//将流加载到解析器中,获取document对象
Document document = builder.build(in);
//获取根节点
Element root = document.getRootElement();
//创建一个person节点
Element person = new Element("person");
//person节点添加节点属性
person.setAttribute("id",String.valueOf("6"));
Element id = new Element("id");//创建id节点
//这里说明一下,new CDATA()生成xml后节点是:<id><![CDATA[value]]></id>
id.addContent(new CDATA(String.valueOf("6")));
//如果使用的是setText(),生成xml则是正常的value <id>value</id>
// id.setText("1");
person.addContent(id);
Element name = new Element("name");//创建name节点
// name.setText("小明");//学生姓名
name.addContent(new CDATA("JACK"));
person.addContent(name);
Element card = new Element("card");//创建card节点
// card.setText("1001");//card存为学号
card.addContent(new CDATA("1006"));
person.addContent(card);
root.addContent(person);
Format format = Format.getCompactFormat();
format.setIndent(" ");//设置换行Tab或空格
format.setEncoding("UTF-8");//设置字符
//创建Xml输出对象XMLOutputter
XMLOutputter outputter = new XMLOutputter(format);
//用outputter 将document 转成xml文件
outputter.output(document,new FileOutputStream("person.xml"));
System.out.println("追加节点成功!");
}catch (Exception e){
e.printStackTrace();
}
}
4.删除Xml节点
/**
* 删除XMl节点
*/
public static void deleteXml(){
try{
File file = new File("person.xml");
SAXBuilder builder = new SAXBuilder();
InputStream in = new FileInputStream(file);
Document document = builder.build(in);
//获取根节点
Element root = document.getRootElement();
List<Element> personList = root.getChildren("person");
for (Element element : personList) {
String id = element.getAttributeValue("id");
if(id.equals("6")){
root.removeContent(element);
break;
}
}
Format format = Format.getCompactFormat();
format.setIndent(" ");//设置换行Tab
format.setEncoding("UTF-8");
XMLOutputter outputter = new XMLOutputter(format);
//利用outputer将document转换成xml文档
outputter.output(document,new FileOutputStream(file));
System.out.println("删除节点成功!");
}catch (Exception e){
e.printStackTrace();
}
}
测试:
public static void main(String[] arg0){
// createXml();
// analyzeXml();
// addToXml();
// deleteXml();
}