使用jdom简单操作XML
2014-01-13 17:35
399 查看
来自: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 } }
------------------------------------------------------------------------------
相关文章推荐
- 简单使用JDOM解析XML
- 使用JDOM解析XML(转载,简单又详细)
- Java中最简单的把数据写入xml文件的方法----jdom之xml操作
- JDOM操作XML简单示例
- 使用JDom操作XML
- 使用JDOM操作XML系列文章一
- 简单使用JDOM解析XML
- [收藏]使用DOM操作XML文档的简单范例代码
- 使用dom4j做简单的xml操作
- 使用xmltextreader对xml进行简单的读取和写入操作
- Java使用JDOM解析XML(转载,简单又详细)
- jdom使用Xpath操作xml
- 使用JDOM操作XML
- XML简单操作类、只可操作单行数据(可直接使用)
- 一个简单的使用jdom解析xml例子
- 使用JDOM解析XML(转载,简单又详细)
- 简单使用dom4j对xml文件进行输出操作(如何使用dom4j对xml文件进行输出)
- 使用Jdom对xml文件进行基本操作
- 使用JDOM2.0.4 操作/解析xml
- java30.XML操作------使用Dom4j简单操作XML数据举例