package xmldom;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.LinkedList;
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.XMLOutputter;
/**
* xml的增删改查之SAXBuilder
* @author Administrator
*
*/
public class XmlTest {
//查询所有的数据
public static void list() throws JDOMException, IOException {
SAXBuilder builder = new SAXBuilder();
InputStream file = new FileInputStream("./src/xmldom/message.xml");
//System.out.println(file.available());
//获得文档对象
Document document = builder.build(file);
//获得根节点
Element root = document.getRootElement();
List list = root.getChildren();
System.out.println("root : "+root);
System.out.println(root.getName());
Iterator it = list.iterator();
//for(Element e:list) {
// for(int i = 0; i < list.size(); i++){
while(it.hasNext()){
Element e = (Element) it.next();
System.out.println("ID: "+e.getAttributeValue("id"));
System.out.println(e.getChildText("username"));
System.out.println(e.getChildText("password"));
//System.out.println(list.iterator().getClass().toString());
}
}
//添加数据
/*
public static void add() throws JDOMException, FileNotFoundException, IOException {
SAXBuilder builder = new SAXBuilder();
Document document = builder.build("src/po.xml");
Element root = document.getRootElement();
Element element = new Element("person");
element.addAttribute("id","3");
Element e1 = new Element("username");
e1.setText("hello");
Element e2 = new Element("password");
e2.setText("world");
element.addContent(e1);
element.addContent(e2);
root.addContent(element);
document.setRootElement(root);
XMLOutputter output = new XMLOutputter();
output.output(document,new FileOutputStream("src/po.xml"));
}
*/
//修改数据
public static void edit(int id) throws JDOMException, FileNotFoundException, IOException {
SAXBuilder builder = new SAXBuilder();
Document document = builder.build("./src/xmldom/message.xml");
Element root = document.getRootElement();
List list = root.getChildren();
Iterator it = list.iterator();
for(int i = 0; i < list.size(); i++) {
Element e = (Element) it.next();
System.out.println("=============="+e.getAttributeValue("id"));
if(Integer.parseInt(e.getAttributeValue("id")) == id) {
e.getChild("username").setText("wuchao");
e.getChild("username").setAttribute("ip2", "66666666666");
e.getChild("password").setText("jiayou");
}
}
XMLOutputter output = new XMLOutputter();
output.output(document,new FileOutputStream("./src/xmldom/message.xml"));
}
//删除
public static void del(int id) throws JDOMException, FileNotFoundException, IOException {
SAXBuilder builder = new SAXBuilder();
Document document = builder.build("./src/xmldom/message.xml");
Element root = document.getRootElement();
List list = root.getChildren();
Iterator it = list.iterator();
for(int i = 0; i < list.size(); i++) {
Element e = (Element) it.next();
if(Integer.parseInt(e.getAttributeValue("id")) == id) {
root.removeContent(e);
break;
}
}
//文件处理
XMLOutputter out = new XMLOutputter();
out.output(document, new FileOutputStream("./src/xmldom/message.xml"));
}
public static void main(String[] args) {
// XmlTest.add();
// XmlTest.edit(1);
try {
XmlTest.list();
} catch (JDOMException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
try {
try {
XmlTest.edit(1);
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
} catch (JDOMException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
/*
<?xml version="1.0" encoding="UTF-8"?>
<root>
<person id="1">
<username ip="1234567" ip2="66666666666"/>
<password>jiayou</password>
</person>
<person id="2">
<username>111111111</username>
<password>password</password>
</person>
</root>
*/
xml的增删改查之SAXBuilder
最新推荐文章于 2024-11-07 22:17:08 发布