来自:http://splend.blog.51cto.com/3717743/984811
package examples.ftp;
/**
*
* 引入jar包:
* 1、commons-io-2.4.jar
* 2、commons-net-3.3.jar
* 3、jdom-2.0.2.jar
*
* */
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import javax.swing.text.html.HTMLDocument.Iterator;
import org.jdom2.Attribute;
import org.jdom2.Comment;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
public class CopyOfMyXML {
/**
* @param args
* @throws IOException
* @throws FileNotFoundException
* @throws JDOMException
*/
public static void main(String[] args) throws FileNotFoundException,
IOException, JDOMException {
new CopyOfMyXML().createXMLFile();
new CopyOfMyXML().add();
new CopyOfMyXML().traverse();
new CopyOfMyXML().del(001);
new CopyOfMyXML().edit(002);
}
public void edit(int i) throws JDOMException, IOException{
SAXBuilder builder = new SAXBuilder();
String path = "jdom.xml";
Document document = builder.build(new File(path));
Element root = document.getRootElement();
@SuppressWarnings("unchecked")
List<Element> list = root.getChildren();
java.util.Iterator<Element> it = list.iterator();
while (it.hasNext()) {
Element e = it.next();
if (Integer.parseInt(e.getAttributeValue("id")) == i) {
e.getChild("name").setText("佳佳");
e.getChild("address").setText("安徽");
}
}
XMLOutputter out = new XMLOutputter();
out.setFormat(out.getFormat().setEncoding("utf-8").setIndent(" "));
out.output(document, new FileOutputStream(path));
}
/**
* 操作XML:删除一个节点
*
* @throws IOException
* @throws JDOMException
* */
public void del(int i) throws JDOMException, IOException {
SAXBuilder builder = new SAXBuilder();
Document document = builder.build(new File("jdom.xml"));
Element root = document.getRootElement();
List<Element> list = root.getChildren();
java.util.Iterator<Element> it = list.iterator();
while (it.hasNext()) {
Element e = it.next();
if (Integer.parseInt(e.getAttributeValue("id")) == i) {
root.removeContent(e);
break;
}
}
XMLOutputter out = new XMLOutputter();
out.setFormat(out.getFormat().setEncoding("utf-8").setIndent(" "));
out.output(document, new FileOutputStream("jdom.xml"));
}
/**
* 遍历XML
*
* @throws IOException
* @throws JDOMException
* */
public void traverse() throws JDOMException, IOException {
SAXBuilder builder = new SAXBuilder();
Document document = builder.build(new File("jdom.xml"));
Element root = document.getRootElement();
List<Element> list = root.getChildren();
java.util.Iterator<Element> it = list.iterator();
while (it.hasNext()) {
Element e = it.next();
System.out.println("编号:" + e.getAttributeValue("id")
+ "性别:" // 获取属性
+ e.getAttributeValue("gender") + "姓名:"
+ e.getChild("name").getText() + "地址:" // 获取值
+ e.getChild("address").getText());
}
}
/**
* 操作本地XML:为本地的XML文档增加一个节点
*
* @throws IOException
* @throws JDOMException
* */
public void add() throws JDOMException, IOException {
SAXBuilder builder = new SAXBuilder();
Document document = builder.build(new File("jdom.xml"));
Element root = document.getRootElement();
Element person = new Element("person");
person.setAttribute("id", "003").setAttribute("gender", "male");
root.addContent(person);
Element name = new Element("name");
name.setText("王力宏");
person.addContent(name);
Element address = new Element("address");
address.setText("中国");
person.addContent(address);
XMLOutputter out = new XMLOutputter();
out.setFormat(out.getFormat().setEncoding("utf-8").setIndent(" "));
out.output(document, new FileOutputStream(new File("jdom.xml")));
}
/**
* 在本地创建一个XML文件
* */
public void createXMLFile() throws FileNotFoundException, IOException {
// 创建文档
Document document = new Document();
// 创建根元素
Element people = new Element("people");
document.addContent(people);
// 创建注释
Comment rootComment = new Comment("将数据从程序输出到XML中!");
people.addContent(rootComment);
Element person1 = new Element("person");
people.addContent(person1);
// 设置属性
person1.setAttribute("id", "001");
Attribute person1_gender = new Attribute("gender", "male");
person1.setAttribute(person1_gender);
Element person1_name = new Element("name");
// 设置值
person1_name.setText("刘德华");
person1.addContent(person1_name);
Element person1_address = new Element("address");
person1_address.setText("香港");
person1.addContent(person1_address);
Element person2 = new Element("person");
people.addContent(person2);
// 设置属性
person2.setAttribute("id", "002").setAttribute("gender", "male");// 添加属性,可以一次添加多个属性
Element person2_name = new Element("name");
person2_name.setText("林志颖");
person2.addContent(person2_name);
Element person2_address = new Element("address");
person2_address.setText("台湾");
person2.addContent(person2_address);
// 设置xml输出格式
Format format = Format.getPrettyFormat();
format.setEncoding("utf-8");// 设置编码
format.setIndent(" ");// 设置缩进
// 得到xml输出流
XMLOutputter out = new XMLOutputter(format);
// 将数据转为XML字符串输出
// System.out.println(out.outputString(document));
// 把数据输出到xml文件中
out.output(document, new FileOutputStream("jdom.xml"));// 或者FileWriter
}
}
------------------------------------------------------------------------------