CreateXml类:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
public class CreateXml extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
String section1 = req.getParameter("section1");
String section2 = req.getParameter("section2");
String section3 = req.getParameter("section3");
String section4 = req.getParameter("section4");
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
document.setXmlVersion("1.0");
Element sections = document.createElement("sections");// 创建根节点元素
document.appendChild(sections);// 创建根节点sections
for (int k = 1; k <= 4; k++) {// 创建子节点author
sections.appendChild(document.createElement("section" + k));
}
NodeList nodeList = document.getElementsByTagName("section1");
// 创建相应的文本节点(内容)
createNodeContent(document, nodeList, section1);
nodeList = document.getElementsByTagName("section2");
// 创建相应的文本节点(内容)
createNodeContent(document, nodeList, section2);
nodeList = document.getElementsByTagName("section3");
// 创建相应的文本节点(内容)
createNodeContent(document, nodeList, section3);
nodeList = document.getElementsByTagName("section4");
// 创建相应的文本节点(内容)
createNodeContent(document, nodeList, section4);
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
File file = new File("d:\\sectionInfoTable.xml");
FileOutputStream out = new FileOutputStream(file);
StreamResult xmlResult = new StreamResult(out);
transformer.transform(domSource, xmlResult);
}
catch (Exception e)
{
System.out.println(e);
}
}
/**
*
* 方法createNodeContent 用于创建指定节点的文本内容(文本节点)
*
* 参数document 标识xml文档 nodeList存放满足条件的节点集合 text用于存放待创建的文本内容
*
*/
private void createNodeContent(Document document, NodeList nodeList,
String text)
{
int size = nodeList.getLength();// 获得列表中的节点数
for (int k = 0; k < size; k++)// 遍历列表每个节点
{
Node node = nodeList.item(k);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element elementNode = (Element) node;
elementNode.appendChild(document.createTextNode(text));
}
}
}
}
web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>CreateXml</servlet-name>
<servlet-class>CreateXml</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CreateXml</servlet-name>
<url-pattern>/WriteXml.do</url-pattern>
</servlet-mapping>
</web-app>
index.jsp文件:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</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>
<form method="post" action="WriteXml.do" >
section1:<input type="text" name="section1"/>
section2:<input type="text" name="section2"/>
section3:<input type="text" name="section3"/>
section4:<input type="text" name="section4"/></br>
<input type="submit" value="提交"/>
</form>
</body>
</html>