JSP如何将HTML的表单数据提交生成一个XML文件

JSP如何将HTML的表单数据提交生成一个XML文件,第1张

JSP将HTML的表单数据提交生成一个XML文件的方法:

1、定义表单,封装id,firstName和lastName

<form:form modelAttribute="person">

<form:hidden path="id" />

<fieldset>

<div class="form-row">

<label for="firstName"><fmt:message key="person.form.firstName"/>:</label>

<span class="input"><form:input path="firstName" /></span>

</div>

<div class="form-row">

<label for="lastName"><fmt:message key="person.form.lastName"/>:</label>

<span class="input"><form:input path="lastName" /></span>

</div>

<div class="form-buttons">

<div class="button">

<input type="submit" id="save" name="_eventId_save" value="<fmt:message key="button.save"/>"/> 

<input type="submit" name="_eventId_cancel" value="Cancel"/> 

</div>

</div>

</fieldset>

</form:form>

2、提交到servlet后,调用构造xml的java方法:

try {

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance()

DocumentBuilder docBuilder = docFactory.newDocumentBuilder()

// root elements

Document doc = docBuilder.newDocument()

Element rootElement = doc.createElement("company")

doc.appendChild(rootElement)

// staff elements

Element staff = doc.createElement("Staff")

rootElement.appendChild(staff)

// set attribute to staff element

Attr attr = doc.createAttribute("id")

attr.setValue("1")

staff.setAttributeNode(attr)

// shorten way

// staff.setAttribute("id", "1")

// firstname elements

Element firstname = doc.createElement("firstname")

firstname.appendChild(doc.createTextNode("yong"))

staff.appendChild(firstname)

// lastname elements

Element lastname = doc.createElement("lastname")

lastname.appendChild(doc.createTextNode("mook kim"))

staff.appendChild(lastname)

// nickname elements

Element nickname = doc.createElement("nickname")

nickname.appendChild(doc.createTextNode("mkyong"))

staff.appendChild(nickname)

// salary elements

Element salary = doc.createElement("salary")

salary.appendChild(doc.createTextNode("100000"))

staff.appendChild(salary)

// write the content into xml file

TransformerFactory transformerFactory = TransformerFactory.newInstance()

Transformer transformer = transformerFactory.newTransformer()

DOMSource source = new DOMSource(doc)

StreamResult result = new StreamResult(new File("d:\\person.xml"));

transformer.transform(source, result)

System.out.println("File saved!")

} catch (ParserConfigurationException pce) {

pce.printStackTrace()

} catch (TransformerException tfe) {

tfe.printStackTrace()

}

3、构造完成:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>

<company>

<staff id="1">

<firstname>yong</firstname>

<lastname>mook kim</lastname>

<nickname>mkyong</nickname>

<salary>100000</salary>

</staff>

</company>

以下是用dom4j来解析xml文件的,如果要运行以下代码,必须导入dom4j的jar包。

import java.io.File

import java.util.Iterator

import org.dom4j.Document

import org.dom4j.DocumentHelper

import org.dom4j.Element

import org.dom4j.io.SAXReader

public class DomTest {

/**

* @param args

*/

public static void main(String[] args) {

try {

//读取xml文件信息

File inputXml=new File("D:/test.xml")

SAXReader saxReader = new SAXReader()

Document document = saxReader.read(inputXml)

//获取根节点<xml>元素

Element root = document.getRootElement()

//创建xml文档

Document doc = DocumentHelper.createDocument()

//创建表格的根节点<table>元素

Element rootElement = DocumentHelper.createElement("table")

doc.setRootElement(rootElement)

//获取根节点<xml>元素的子节点<row>元素列表

Iterator iter = root.elementIterator("row")

//循环列表

while(iter.hasNext()) {

//得到每个<row>元素

Element rowEle=(Element)iter.next()

//创建表格中的行<tr>元素

Element trElement = rootElement.addElement("tr")

//创建表格中行<tr>元素的子元素列<td>元素

//第一列存储<row>元素的ballsnum属性值

Element ballsnum = trElement.addElement("td")

ballsnum.addText(rowEle.attributeValue("ballsnum"))

//第二列存储<row>元素的opentime属性值

Element opentime = trElement.addElement("td")

opentime.addText(rowEle.attributeValue("opentime"))

//第三列存储<row>元素的opencode属性值

Element opencode = trElement.addElement("td")

opencode.addText(rowEle.attributeValue("opencode"))

//第四列存储<row>元素的expect属性值

Element expect = trElement.addElement("td")

expect.addText(rowEle.attributeValue("expect"))

}

System.out.println(doc.asXML())

} catch (Exception e) {

e.printStackTrace()

}

}

}

我将

<?xml version="1.0" encoding="UTF-8"?>

<xml>

<row ballsnum="3" opentime="2014-01-19 21:30:00" opencode="05,14,16,21,29,30|12" expect="14008"/>

<row ballsnum="3" opentime="2014-01-16 21:30:00" opencode="08,10,12,14,18,28|14" expect="14007"/>

</xml>

存储在D盘下的test.xml文件中

我读取D盘下的test.xml文件中的xml内容生成了一个表格

System.out.println(doc.asXML())语句将在控制台打印表格内容

打印结果如下:

<?xml version="1.0" encoding="UTF-8"?>

<table>

<tr>

<td>3</td>

<td>2014-01-19 21:30:00</td>

<td>05,14,16,21,29,30|12</td>

<td>14008</td>

</tr>

<tr>

<td>3</td>

<td>2014-01-16 21:30:00</td>

<td>08,10,12,14,18,28|14</td>

<td>14007</td>

</tr>

</table>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/6197030.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-17
下一篇 2023-03-17

发表评论

登录后才能评论

评论列表(0条)

保存