JDOM操作XML资料

JDOM操作XML文件
本示例使用JDOM对XML文件进行了简单的增、删、查、改操作,使用时须把jdom.jar包引入工程:通过该例题,可以对XML文件的使用有进一步的了解。

XML文件:users.xml

<?xml version="1.0" encoding="GBK"?>
<root>
  <person id="1">
    <username ip="11111">xiaoma</username>
    <password>xiaoma</password>
  </person>
  <person id="2">
    <username ip="22222">manager</username>
    <password>password2</password>
  </person>
</root>

java程序:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

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;

public class XMLDemo { //JDOM操作XML文件示例

private File filename; //XML文件路径

public XMLDemo(File filename){
this.filename=filename;
}

public Document getDoc(){
Document doc=null;
try {
if(filename.exists()){
SAXBuilder builder=new SAXBuilder();
doc=builder.build(filename);
}
return doc;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

//添加元素
public void add() throws Exception{
Document doc=getDoc();
Element root=doc.getRootElement();

Element newE=new Element("person");
newE.setAttribute("id","3");

newE.addContent(new Element("username").setText("Falcon").setAttribute("ip","221"));
newE.addContent(new Element("password").setText("991"));

root.addContent(newE);

Format format=Format.getPrettyFormat();
format.setEncoding("GBK");
XMLOutputter output=new XMLOutputter(format);
output.output(doc, new FileOutputStream(filename));
}

//显示所有元素
public void list() throws Exception{
Document doc=getDoc();
Element root=doc.getRootElement();

List list=root.getChildren();
Iterator it=list.iterator();
while(it.hasNext()){
Element element=(Element)it.next();
System.out.println("-------------------");
System.out.println("Person's id="+element.getAttributeValue("id"));
System.out.println("Username:"+element.getChild("username").getText()+" ip="+element.getChild("username").getAttributeValue("ip"));
System.out.println("password:"+element.getChild("password").getText());
}
}

//修改元素
public void edit(int id) throws Exception{
Document doc=getDoc();
Element root=doc.getRootElement();

List list=root.getChildren();
Iterator it=list.iterator();
while(it.hasNext()){
Element element=(Element)it.next();
if(Integer.parseInt(element.getAttributeValue("id"))==id){
element.getChild("password").setText("222");
break;
}
}

Format format=Format.getPrettyFormat();
format.setEncoding("GBK");
XMLOutputter output=new XMLOutputter(format);
output.output(doc, new FileOutputStream(filename));
}

//删除元素
public void delete(int id) throws Exception{
Document doc=getDoc();
Element root=doc.getRootElement();

List list=root.getChildren();
Iterator it=list.iterator();
while(it.hasNext()){
Element element=(Element)it.next();
String uid=element.getAttributeValue("id");
if(Integer.parseInt(uid)==id){
root.removeContent(element);
break;
}
}

Format format=Format.getPrettyFormat();
format.setEncoding("GBK");
XMLOutputter output=new XMLOutputter(format);
output.output(doc, new FileOutputStream(filename));
}

public static void main(String[] args){
try {
new XMLDemo(new File("./users.xml")).list();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}