接口:
package com.baozupo.gzl.severce;
import org.dom4j.Document;
import com.baozupo.gzl.bean.Froms;
public interface XMLUtilsSeverce {
/**
* 获取XML中的信息
* @return
*/
public Froms getAllUser(String wflowTitle);
/**
* 向XML文件中添加一个user
* @param user
*/
public void add(String wflowTitle,String inputData);
/**
* 根据id删除user
* @param id
*/
public void deleteById(String id);
/**
* 修改user信息
* @param user
*/
public void update(String wflowTitle,String inputData);
}
实例化:
package com.baozupo.gzl.severceImpl;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.springframework.stereotype.Service;
import com.baozupo.gzl.bean.Froms;
import com.baozupo.gzl.severce.XMLUtilsSeverce;
@Service
public class XMLUtilsSeverceImpl implements XMLUtilsSeverce {
/**
* 获取XML中所有的用户信息
* @return
*/
public Froms getAllUser(String wflowTitle){
Document document=getDocument();
Element rootElement=document.getRootElement();
List<Element> userElements=rootElement.elements();
Froms from=new Froms();
for (Element userElement : userElements) {
if(wflowTitle.equals(userElement.element("wflowTitle").getTextTrim())){
from.setThisName(userElement.element("wflowTitle").getTextTrim());
from.setThisValue(userElement.element("inputData").getTextTrim());
}
}
return from;
}
/**
* 向XML文件中添加一个user
* @param user
*/
public void add(String wflowTitle,String inputData){
File file =new File("D:\\java_\\user.xml");
Document document=null;
if(file.exists())
{
document=getDocument();
}else{
document=DocumentHelper.createDocument();
Element root = document.addElement("gooflows");
}
Element rootElement=document.getRootElement();
Element userElement=rootElement.addElement("flow");
if(file.exists()){
userElement.addAttribute("id",getMaxId()+1+"");
}else{
userElement.addAttribute("id",1+"");
}
Element nameElement=userElement.addElement("wflowTitle");
Element ageElement=userElement.addElement("inputData");
nameElement.setText(wflowTitle);
ageElement.setText(inputData);
if(file.exists()){
write2XML(document);
}else{
saveDocumentToFile(document);
}
}
/**
* 根据id删除user
* @param id
*/
public void deleteById(String id){
Document document=getDocument();
Element rootElement=document.getRootElement();
List<Element> userElements=rootElement.elements("user");
for (Element userElement : userElements) {
if(userElement.attributeValue("id").equals(id)){
System.out.println("开始删除.....");
rootElement.remove(userElement);
System.out.println("删除结束.....");
}
}
write2XML(document);
}
/**
* 修改user信息
* @param user
*/
public void update(String wflowTitle,String inputData){
Document document=getDocument();
Element rootElement=document.getRootElement();
List<Element> userElements=rootElement.elements();
for (Element userElement : userElements) {
if(wflowTitle.equals(userElement.element("wflowTitle").getTextTrim())){
List<Element> elements=userElement.elements();
userElement.element("wflowTitle").setText(wflowTitle);
userElement.element("inputData").setText(inputData);
}
}
write2XML(document);
}
/**
* 获取根节点
* @return rootElement
*/
public static Document getDocument(){
try {
SAXReader saxReader=new SAXReader();
// InputStream in=OperateXML.class.getResourceAsStream("/user.xml");
Document document=saxReader.read("D:\\java_\\user.xml");
return document;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 把内容写到XML文件中
* @param document
*/
public static void write2XML(Document document){
try {
OutputFormat format=new OutputFormat(" ", true, "utf-8");
format.setTrimText(true);
XMLWriter writer = new XMLWriter(format);
writer.setOutputStream(new FileOutputStream("D:\\java_\\user.xml"));
writer.write(document);
writer.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 获取xml文件中userId的最大值
* @return int
*/
public static int getMaxId(){
int maxId=0;
Document document=getDocument();
Element rootElement=document.getRootElement();
List<Element> userElements=rootElement.elements();
for (Element element : userElements) {
int id=Integer.valueOf(element.attributeValue("id"));
if(maxId<id){
maxId=id;
}
}
return maxId;
}
/**
* 方法描述:<b>存储完整XML文件.</b></br>
* 备 注: </br>
* 创 建 人: zyl</br>
* 创建日期:2013-3-18</br>
*
* @param document
* @param xmlFilePath
* @param xmlFileName
* @param isTrimText
* @param xmlEncoding
*/
public static void saveDocumentToFile(Document document) {
File file = new File("D:\\java_");
// 判断路径是否存在,不存在创建
if (!file.exists()) {
file.mkdirs();
}
// 保存文件
OutputFormat format = null;
format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");// UTF-8
try {
org.dom4j.io.XMLWriter xmlWriter = new org.dom4j.io.XMLWriter(
new FileOutputStream("D:\\java_\\user.xml"), format);// FileOutputStream可以使UTF-8生效
xmlWriter.write(document);
xmlWriter.close();
} catch (IOException e) {
System.out.println("保存XML异常:" + e.getMessage());
System.out.println("正在保存的文件名是:user.xml");
}
// 存到文件中结束
}
}