使用Jdom对xml文件进行基本操作
2014-09-18 15:20
513 查看
使用Jdom对xml文件进行基本操作演示
注:创建后的XML文件形式如下,假设ID为主键:
首先创建一个Student类,包含了student的基本属性
对XML进行基本的操作,一些其它的操作类似,在此就没有写出
附上源码,包含Jdom包,API文档需要的可到官网下载。
xmloperation.rar (147.3 KB)
下载次数: 44
注:创建后的XML文件形式如下,假设ID为主键:
<?xml version="1.0" encoding="UTF-8"?> <class> <student id="2009"> <name>coolszy</name> <gender>boy</gender> <birthday>1988-01-01</birthday> </student> </class>
首先创建一个Student类,包含了student的基本属性
package com.szy.xmloperation; public class Student { private String id; private String name; private String gender; private String birthday; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } }
对XML进行基本的操作,一些其它的操作类似,在此就没有写出
package com.szy.xmloperation; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.util.List; import org.jdom.Document; import org.jdom.Element; import org.jdom.input.SAXBuilder; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; import org.jdom.output.Format.TextMode; public class XMLOperation { /** * 创建xml文件 * @param xml文件的路径+文件名 * @throws Exception */ public void create(String filePath) throws Exception { Document doc=null; Element root=null; root=new Element("class"); //创建跟节点 doc=new Document(root); outPut(doc, filePath); //输出到xml文件中 } /** * 往xml文件中添加新节点 * @param Student对象 * @param xml文件的路径+文件名 * @throws Exception */ public void addNode(Student student,String filePath) throws Exception { Document doc=null; Element root=null; SAXBuilder sb=new SAXBuilder(false); doc=sb.build(new FileInputStream(new File(filePath))); root=doc.getRootElement(); //获取根元素 Element studentElement=new Element("student"); studentElement.setAttribute("id",student.getId()); Element namElement=new Element("name"); namElement.addContent(student.getName()); studentElement.addContent(namElement); Element genderElement=new Element("gender"); genderElement.addContent(student.getGender()); studentElement.addContent(genderElement); Element birthdayElement=new Element("birthday"); birthdayElement.addContent(student.getBirthday()); studentElement.addContent(birthdayElement); root.addContent(studentElement); outPut(doc, filePath); } /** * 修改节点属性的值 * @param 属性名 * @param 原来的值 * @param 新的值 * @param xml文件的路径+文件名 * @throws Exception */ public void modifyProperty(String propertyName,String oldValue,String newValue,String filePath) throws Exception { Document doc=null; Element root=null; SAXBuilder sb=new SAXBuilder(false); doc=sb.build(new FileInputStream(new File(filePath))); root=doc.getRootElement(); //获取根元素 List<Element> list=root.getChildren("student"); for (Element element : list) { if (element.getAttributeValue(propertyName).equals(oldValue)) { element.setAttribute(propertyName, newValue); break; } } outPut(doc, filePath); } public void deleteNode(String id,String filePath) throws Exception { Document doc=null; Element root=null; SAXBuilder sb=new SAXBuilder(false); doc=sb.build(new FileInputStream(new File(filePath))); root=doc.getRootElement(); //获取根元素 List<Element> list=root.getChildren("student"); for (Element element : list) { if (element.getAttributeValue("id").equals(id)) { root.removeContent(element); break; } } outPut(doc, filePath); } /** * 把结果保存到xml文件中 * @param doc Document对象 * @param filePath xml文件路径+文件名 * @throws Exception */ private void outPut(Document doc,String filePath) throws Exception { XMLOutputter output=new XMLOutputter(); Format format=output.getFormat(); format.setEncoding("UTF-8"); //设置编码 format.setIndent("\t"); format.setLineSeparator(System.getProperty("line.separator")); format.setTextMode(TextMode.TRIM_FULL_WHITE); //去掉后观察效果 format.setExpandEmptyElements(true); output.setFormat(format); FileWriter writer=new FileWriter(filePath); output.output(doc, writer); writer.close(); } }
附上源码,包含Jdom包,API文档需要的可到官网下载。
xmloperation.rar (147.3 KB)
下载次数: 44
相关文章推荐
- SQL SERVER 2005中使用sql语句对xml 文件和其数据的进行操作(很全面)
- [绍棠] 使用NSFileHandle类对文件进行基本操作,IOS文件操作
- 解析XML文件——JDOM基本操作
- 简单使用dom4j对xml文件进行输出操作(如何使用dom4j对xml文件进行输出)
- SQLSERVER 2005中使用sql语句对xml文件和其数据的进行操作(很全面)
- Objective-C使用NSFileHandle类对文件进行基本操作,IOS文件操作
- java中以JDOM4的方式来对xml文件对象进行解析操作
- 使用DOM操作XML文件之DOM下基本API
- 使用MSXML进行基本的XML操作
- 使用common-configuration进行对配置文件的操作(xml,properties)
- 数据库操作_连接SQL Server数据库示例;连接ACCESS数据库;连接到 Oracle 数据库示例;SqlCommand 执行SQL命令示例;SqlDataReader 读取数据示例;使用DataAdapter填充数据到DataSet;使用DataTable存储数据库表;将数据库数据填充到 XML 文件;10 使用带输入参数的存储过程;11 使用带输入、输出参数的存储过程示;12 获得数据库中表的数目和名称;13 保存图片到SQL Server数据库示例;14 获得插入记录标识号;Exce
- [转]JDOM操作XML文件
- java中使用jdom生成百度新闻抓取的xml文件
- 使用IRP进行文件操作
- 使用IRP进行文件操作
- 使用jdom操作xml数据,生成含Jtree的applet(转载 Jagie 原创 )
- 如何在带有名称空间的xml文件中进行存取操作
- JDOM操作XML文件
- 使用JDom操作XML
- 使用yacc对文件进行操作