package org.dom4j.xml.parse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
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.SAXReader;
import org.dom4j.io.XMLWriter;
public class XMLParse {
public static void main(String[] args) {
// try{
// File file=new File("d:/students.xml");
// OutputStream out=new FileOutputStream(file);
// XMLParse.createXMLDocument(out);
// }catch(FileNotFoundException e)
// {
// e.printStackTrace();
// }
File file=new File("d:/students.xml");
XMLParse.modifyXMLDocument(file);
}
/**
* 根据给定的输出流来创建xml文档
* @author Administrator
* @version hw20140925
* @since 2014-09-25
* @param out 创建后的xml文档保存到该输出流中,实现将xml文档保存到某个物理地址
*/
public static void createXMLDocument(OutputStream out)
{
//文档
Document document = DocumentHelper.createDocument();
//创建根元素students
Element StudentsElement = document.addElement("students");
//创建第一个student元素
Element studentElement = StudentsElement.addElement("student");
//为第一个student元素添加子元素
Element nameElement = studentElement.addElement("name");
nameElement.setText("彥男");
Element ageElement = studentElement.addElement("age");
ageElement.setText("23");
Element sexElement = studentElement.addElement("sex");
sexElement.setText("男");
Element texEleemnt = studentElement.addElement("tel");
texEleemnt.setText("12400252202");
Element emailElement = studentElement.addElement("email");
emailElement.setText("yannan@163.com");
Element faxElement = studentElement.addElement("fax");
faxElement.setText("298393");
//创建第二个student元素
Element studentElement1 = StudentsElement.addElement("student");
//为第二个student元素添加子元素
Element nameElement1 = studentElement1.addElement("name");
nameElement1.setText("麗麗");
Element ageElement1 = studentElement1.addElement("age");
ageElement1.setText("20");
Element sexElement1 = studentElement1.addElement("sex");
sexElement1.setText("女");
Element texEleemnt1 = studentElement1.addElement("tel");
texEleemnt1.setText("18223255902");
Element emailElement1 = studentElement1.addElement("email");
emailElement1.setText("lilihxn@163.com");
Element faxElement1 = studentElement1.addElement("fax");
faxElement1.setText("247393");
//将文档写到磁盘上
OutputFormat format=OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
try {
XMLWriter writer=new XMLWriter(out,format);
writer.write(document);
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch(IOException e)
{
e.printStackTrace();
}
}
/**
* @author Administrator
* @version hw20140925
* @since 2014-09-25
* @param file 修改后的xml文件存储物理路径
*/
public static void modifyXMLDocument(File file)
{
Document document = null;
OutputStream out = null;
XMLWriter writer = null;
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
//加载某个xml文档
SAXReader reader=new SAXReader();
try
{
document = reader.read(file);
//通过文档,加载需要修改的某个节点
@SuppressWarnings("unchecked")
List<Element> elementList = (List<Element>) document.selectNodes("//name");
for(Element ele : elementList)
{
ele.setText("修改后的值是***");
}
//在一次将修改后的文档写回到磁盘
try
{
out=new FileOutputStream(new File("d:/modify-studnet.xml"));
writer = new XMLWriter(out , format);
writer.write(document);
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
}catch(DocumentException e)
{
e.printStackTrace();
}catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}catch(IOException e)
{
e.printStackTrace();
}finally
{
try
{
out.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
/**
* 删除某个节点
*/
public static void delXMLDOcumentNode()
{
}
/**
* XML文档遍历
*/
public static void listXMLDocument()
{
}
}