1. 创建一个People对象
package XML;
public class People {
public People() {
// TODO Auto-generated constructor stub
}
private String name;
private int age;
private String sex;
public void tell(){ //取得信息的方法
System.out.println(" 姓名:"+this.getName()+" 年龄:"+this.getAge()+" 性别:"+this.getSex());
}
public People(String name,int age,String sex){
this.setName(name);
this.setAge(age);
this.setSex(sex);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age>=0){
this.age = age;
}else{
this.age = 0;
}
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public static void main(String args[]){
new People("wangwu",-10,"男").tell();
}
}
2. 创建,解析XML及导入批量数据到XML中
package XML;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class XmlOperate {
//创建XML
public void CreateXml(){
Document doc = DocumentHelper.createDocument(); //创建文档 (树形结构)
Element e1 = doc.addElement("province"); //定义根节点 (第一层)
Element e2 = e1.addElement("people"); //定义子节点(第二层)
Element name1 = e2.addElement("name"); //定义子节点(第三层)
Element age1 = e2.addElement("age"); //定义子节点(第三层)
Element sex1 = e2.addElement("sex"); //定义子节点(第三层)
name1.setText("张三"); //设置子节点内容
age1.setText("30");
sex1.setText("男");
Element xe2 = e1.addElement("people"); //定义子节点(第二层)
Element name2 = xe2.addElement("name"); //定义子节点(第三层)
Element age2 = xe2.addElement("age"); //定义子节点(第三层)
Element sex2 = xe2.addElement("sex"); //定义子节点(第三层)
name2.setText("李四"); //设置子节点内容
age2.setText("29");
sex2.setText("女");
OutputFormat format= OutputFormat.createPrettyPrint();
//设置输出格式
format.setEncoding("GBK"); //设置输出编码
try {
XMLWriter write = new XMLWriter(new FileOutputStream("D:"+File.separator+"output.xml"),format); //创建输出文件
write.write(doc); //输出
write.close();
System.out.println("xml文件创建成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
//解析XML
public void ReadXml(){
File file1 = new File("D:"+File.separator+"output.xml");
//读取XML文件对象
SAXReader read=new SAXReader(); //建立SAX解析读取
Document doc = null; //创建文档
try {
doc= read.read(file1);
} catch (DocumentException e) {
e.printStackTrace();
}
//取得根元素
Element root= doc.getRootElement();
//取得全部的子节点
Iterator it1= root.elementIterator();
while(it1.hasNext()){
Element linkname=(Element)it1.next(); //取得一个节点对象
System.out.print("姓名:"+linkname.elementText("name"));
//取得name元素内容
System.out.print(" 年龄:"+linkname.elementText("age"));
//取得age元素内容
System.out.println(" 性别:"+linkname.elementText("sex"));
//取得sex元素内容
}
//取得节点 id="test" 里边的数据
}
//批量数据生成xml
public void ImpAllXml(List<People> people){
Document doc = DocumentHelper.createDocument(); //创建文档
Element root= doc.addElement("shanxi"); //创建根节点int m= people.size();
for(int i=0; i<m; i++){
Element ele =root.addElement("people"); //定义对象节点
ele.addAttribute("id", "test"+(i+1)+""); //增加节点属性
Element name = ele.addElement("name"); //定义属性节点
Element age = ele.addElement("age");
Element sex = ele.addElement("sex");
name.setText(people.get(i).getName()); //为对应节点赋值
age.setText(String.valueOf(people.get(i).getAge()));
sex.setText(people.get(i).getSex());
}
OutputFormat format= OutputFormat.createPrettyPrint();
//设置输出格式
format.setEncoding("GBK"); //设置输出编码
try {
XMLWriter write = new XMLWriter(new FileOutputStream("D:"+File.separator+"test.xml"),format);
write.write(doc);
write.close();
System.out.println("创建text.xml文件成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* xml创建和解析
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
XmlOperate xml = new XmlOperate();
//xml.ReadXml(); //读取xml
//批量数据生成xml
List<People> list = new ArrayList<People>();
list.add(new People("川",28,"男") );
list.add(new People("春",28,"男"));
list.add(new People("小田",28,"男"));
list.add(new People("liu",32,"男"));
list.add(new People("燕",26,"女"));
list.add(new People("郭",29,"男"));
xml.ImpAllXml(list);
}
}
3.相关的XML
<?xml version="1.0" encoding="GBK"?> <province> <people> <name>张三</name> <age>30</age> <sex>男</sex> </people> <people id="test"> <name>李四</name> <age>20</age> <sex>女</sex> </people> </province>
<?xml version="1.0" encoding="GBK"?> <shanxi> <people id="test1"> <name>川</name> <age>28</age> <sex>男</sex> </people> <people id="test2"> <name>春</name> <age>28</age> <sex>男</sex> </people> <people id="test3"> <name>小田</name> <age>28</age> <sex>男</sex> </people> <people id="test4"> <name>liu</name> <age>32</age> <sex>男</sex> </people> <people id="test5"> <name>燕</name> <age>26</age> <sex>女</sex> </people> <people id="test6"> <name>郭</name> <age>29</age> <sex>男</sex> </people> </shanxi>