Jdom将文件读和写xml

来源:互联网 发布:发条js调试工具 编辑:程序博客网 时间:2024/05/17 00:16

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

package org.demo.jdom;

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

import org.jdom.Attribute;
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 DemoJDom {

 /**
  * @param args
  * @throws IOException
  * @throws FileNotFoundException
  * @throws JDOMException
  */
 public static void main(String[] args) throws FileNotFoundException, IOException, JDOMException {
  //xmlJdomWrite() ;
  xmlJdomReader();
 }
 
 public static List<Student>  initStudent(){
  List<Student> stuList=new ArrayList<Student>();
  for (int i = 1; i < 5; i++) {
   stuList.add(new Student("NO"+i, "JDom_"+i, "男", 20+i, "Jdom__"+i));
  }
  return stuList;
 }
 //写xml
 public static void xmlJdomWrite() throws FileNotFoundException, IOException{
  List<Student>  stus=initStudent();
  
  //创建一个xml文档
   Document document=new Document();
  //创建一个根元素
     Element students=new Element("students");
  //将根元素添加到xml文档中
     document.addContent(students);
    
    for (Student s : stus) {
     //创建学员
     Element student=new Element("student");
      //设置学员学号属性
     student.setAttribute(new Attribute("id", s.getId()));
    
     Element studentName=new Element("name").setText(s.getName());
    
     Element studentSex=new Element("sex").setText(s.getSex());
    
     Element studentAge=new Element("age").setText(s.getAge()+"");
    
     Element studentAddress=new Element("address").setText(s.getAddress());
    
     student.addContent(studentName)
            .addContent(studentSex)
            .addContent(studentAge)
            .addContent(studentAddress);
    
     students.addContent(student);
  }
    
    
     XMLOutputter out=new XMLOutputter();
    //设置xml中的格式
     out.setFormat(Format.getPrettyFormat().setIndent("    "));
     //写入xml
     out.output(students, new FileOutputStream(new File("students.xml")));
    
 }
 // 读xml
 public static void xmlJdomReader() throws JDOMException, IOException{
  
  SAXBuilder saxBuilder=new SAXBuilder();
  Document document=saxBuilder.build(new File("students.xml"));
  
  //得到根元素
  Element rootElement=document.getRootElement();
  
  //String students=rootElement.getName();
  
  List<Element> childrens=rootElement.getChildren();
  for (Element e : childrens) {
   //String student=e.getName();
   List<Element> childElements=e.getChildren();
   for (Element element : childElements) {
    String elementName=element.getName();
    String elementValue=element.getValue();
    
    System.out.println(elementName+"="+elementValue);
    
   }
   System.out.println();
  }
  
  
  
 }
}

 

===========================================Student实体类==============================================

package org.demo.jdom;

import java.io.Serializable;

public class Student implements Serializable {
private String id;
private String name;
private String sex;
private int age;
private String address;
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 int getAge() {
  return age;
}
public void setAge(int age) {
  this.age = age;
}
public String getAddress() {
  return address;
}
public void setAddress(String address) {
  this.address = address;
}


public String getSex() {
  return sex;
}
public void setSex(String sex) {
  this.sex = sex;
}
public Student() {
  // TODO Auto-generated constructor stub
}

public Student(String id, String name,String sex, int age, String address) {
  super();
  this.id = id;
  this.name = name;
  this.sex=sex;
  this.age = age;
  this.address = address;
}


}

=========================================运行结果如下==================================================

<students>
    <student id="NO1">
        <name>JDom_1</name>
        <sex>男</sex>
        <age>21</age>
        <address>Jdom__1</address>
    </student>
    <student id="NO2">
        <name>JDom_2</name>
        <sex>男</sex>
        <age>22</age>
        <address>Jdom__2</address>
    </student>
    <student id="NO3">
        <name>JDom_3</name>
        <sex>男</sex>
        <age>23</age>
        <address>Jdom__3</address>
    </student>
    <student id="NO4">
        <name>JDom_4</name>
        <sex>男</sex>
        <age>24</age>
        <address>Jdom__4</address>
    </student>
</students>