//CreateXML.java文件
package com.data;
import org.dom4j.*;
import java.io.File;
import java.io.FileOutputStream;
import java.text.Format;
import javax.lang.model.element.ElementVisitor;
import org.dom4j.io.*;
import org.dom4j.*;
import java.io.*;
import java.util.*;
public class CreateXML {
//无惨构造函数
public CreateXML(){}
//产生一个XML文档并从盘
public Document create(String filename){
Document document=DocumentHelper.createDocument();
Element rootElement=document.addElement("linkmans");
//为根元素创建第一个子元素
Element fristElement=rootElement.addElement("linkman");
Element element1=fristElement.addElement("name");
element1.setText("张三");
Element element2=fristElement.addElement("id");
element2.setText("001");
Element element3=fristElement.addElement("company");
element3.setText("A 公司");
Element element4=fristElement.addElement("email");
element4.setText("(020)234223");
//为根元素添加第二个子元素
Element secondElement=rootElement.addElement("linkman");
Element elementa=secondElement.addElement("name");
elementa.setText("李四");
Element elementb=secondElement.addElement("id");
elementb.setText("002");
Element elementc=secondElement.addElement("company");
elementc.setText("B 公司");
Element elementd=secondElement.addElement("mail");
elementd.setText("3253252");
//将创建的XML文档从盘
try {
XMLWriter output=null;
//创建一个格式化对象
OutputFormat format=OutputFormat.createPrettyPrint();
//使用TAB缩进
format.setIndent(" ");
//创建一个XMLwrite对象
output=new XMLWriter(new FileOutputStream(new File(filename)),format);
//将XML文档输出
output.write(document);
output.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
//返回XML文档对象
return document;
}
//将文本转换为XML文档并存盘
public Document create(String filename,String text)throws Exception {
Document document=DocumentHelper.parseText(text);
//将创建的XMl文档存盘
try {
XMLWriter output=null;
OutputFormat format=OutputFormat.createPrettyPrint();
format.setIndent(" ");
output=new XMLWriter(new FileOutputStream(new File(filename)), format);
output.write(document);
output.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
return document;
}
}
//createXML.jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="org.dom4j.*"%>
<%@ page import="com.data.CreateXML" %>
<jsp:useBean id="myCreate" class="com.data.CreateXML"></jsp:useBean>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>创建XML文件</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h2>创建XMl文件</h2>
<%
//定义一个myfirst.xml文件的绝对物理路径
String fileName1=request.getRealPath("/")+"//myfirst.xml";
//产生并保存myfirst.xml文件并取得xml文档对象
Document doc=myCreate.create(fileName1);
//暂存在XML文档的内容
String reslut=doc.asXML();
//创建一个符合XML文档语法的文本串
StringBuffer text=new StringBuffer();
text.append("<users>");
text.append("<user>");
text.append("<loginname>admin</loginname>");
text.append("<password>123</password>");
text.append("</user>");
text.append("</users>");
//以文本串为内容创建一个XMl文档
String fileName2=request.getRealPath("/")+"//mysecond.xml";
myCreate.create(fileName2,text.toString());
%>
<textarea rows="10" cols="60"><%=reslut%></textarea>
</body>
</html>