<Rowset>
<RowData RowState="add" Row_Number="1000">
<Field name="XM">张三风</Field>
<Field name="SFZBH">220102117005072818</Field>
<Field name="DH">5648436</Field>
<Field name="NL">20</Field>
<Field name="CSRQ">19750518</Field>
</RowData>
<RowData RowState="EDITING" Row_Number="1001">
<Field name="XM">张三风</Field>
<Field name="SFZBH">220102117005070000</Field>
<Field name="DH">5648436</Field>
</RowData>
<RowData RowState="EDITED" Row_Number="1002">
<Field name="XM">张三风</Field>
<Field name="SFZBH">220102117005072818</Field>
<Field name="DH">111111</Field>
<Field name="NL">55</Field>
<Field name="CSRQ">19750516</Field>
</RowData>
</Rowset>
<RowData RowState="add" Row_Number="1000">
<Field name="XM">张三风</Field>
<Field name="SFZBH">220102117005072818</Field>
<Field name="DH">5648436</Field>
<Field name="NL">20</Field>
<Field name="CSRQ">19750518</Field>
</RowData>
<RowData RowState="EDITING" Row_Number="1001">
<Field name="XM">张三风</Field>
<Field name="SFZBH">220102117005070000</Field>
<Field name="DH">5648436</Field>
</RowData>
<RowData RowState="EDITED" Row_Number="1002">
<Field name="XM">张三风</Field>
<Field name="SFZBH">220102117005072818</Field>
<Field name="DH">111111</Field>
<Field name="NL">55</Field>
<Field name="CSRQ">19750516</Field>
</RowData>
</Rowset>
下面的是比较核心的代码片段,向文档中添加元素:
public static void main (String args[]){
Document mapDoc = null;
Document dataDoc = null;
//Create the new Document
Document newDoc = null;
try {
//Create the DocumentBuilderFactory
DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
//Create the DocumentBuilder
DocumentBuilder docbuilder = dbfactory.newDocumentBuilder();
//Parse the file to create the Document
mapDoc = docbuilder.parse("mapping.xml");
//Instantiate a new Document object
dataDoc = docbuilder.newDocument(); //Instantiate the new Document
newDoc = docbuilder.newDocument();
} catch (Exception e) {
System.out.println("Problem creating document: "+e.getMessage());
}
...
//Retrieve the root element (also called "root")
Element newRootInfo = (Element)mapRoot.getElementsByTagName("root").item(0);
//Retrieve the root and row information
String newRootName = newRootInfo.getAttribute("name");
String newRowName = newRootInfo.getAttribute("rowName");
//Retrieve information on elements to be built in the new document
NodeList newNodesMap = mapRoot.getElementsByTagName("element");
//Create the final root element with the name from the mapping file
Element newRootElement = newDoc.createElement(newRootName);
NodeList oldRows = dataRoot.getElementsByTagName("row");
for (int i=0; i < oldRows.getLength(); i++){ //For each of the original rows
Element thisRow = (Element)oldRows.item(i);
//Create the new row
Element newRow = newDoc.createElement(newRowName);
for (int j=0; j < newNodesMap.getLength(); j++) {
//Get the mapping information for each column
Element thisElement = (Element)newNodesMap.item(j);
String newElementName = thisElement.getAttribute("name"); Element oldElement = (Element)thisElement.getElementsByTagName("content").item(0);
String oldField = oldElement.getFirstChild().getNodeValue();
//Get the original values based on the mapping information
Element oldValueElement = (Element)thisRow.getElementsByTagName(oldField).item(0);
String oldValue = oldValueElement.getFirstChild().getNodeValue();
//Create the new element
Element newElement = newDoc.createElement(newElementName);
newElement.appendChild(newDoc.createTextNode(oldValue));
//Add the new element to the new row
newRow.appendChild(newElement); }
//Add the new row to the root
newRootElement.appendChild(newRow);
}
//Add the new root to the document
newDoc.appendChild(newRootElement);
}
}下面,我把算法给你,你自己查找相关资料:
创建新 XML 文档的过程如下:1\解析映射文件以使包括要检索的数据在内的必要信息可用。
2\检索源查询。这允许基于映射文件动态检索数据。
3\将数据存储在 Document 对象中。然后就可以从这个临时文档抽取数据以根据映射创建目标文档。
4\检索数据映射以使它可用于应用程序。
5\循环处理初始数据。分析每一行数据并将其重新映射到新结构中。
6\检索元素映射。映射文件确定以什么样的顺序从临时文档中抽取什么数据。
7\向新文档添加元素。一旦检索了数据,就用新名称将它添加到新文档中。
8\向新文档中添加属性。最后,向适当的元素中添加属性。
package com.hns.bean;
import javax.xml.parsers.*;
import java.io.*;
import org.w3c.dom.*;
import org.apache.crimson.tree.XmlDocument;
public class xmlTest2 {
public static void main(String args[]){
try{
//获得一个xml解析器
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.newDocument();
Node sheet = doc.createProcessingInstruction("xml-stylesheet","type=\"text/xsl\" href=\"style.xsl\"");
doc.appendChild(sheet);
//创建
Element root=doc.createElement("catalog");
//将根元素填加上文档
doc.appendChild(root);
//建立book元素
for(int i=0;i<1;i++){
Element book=doc.createElement("book");
book.setAttribute("id","book"+i+1);
root.appendChild(book);
//建立author,title元素
Element author=doc.createElement("author");
book.appendChild(author);
Text tAuthor=doc.createTextNode("飞碟");
author.appendChild(tAuthor);
Element title=doc.createElement("title");
book.appendChild(title);
Text tTitle=doc.createTextNode("飞碟众书");
title.appendChild(tTitle);
//创建publish_date
Element publish_date=doc.createElement("publish_date");
book.appendChild(publish_date);
Element year=doc.createElement("year");
publish_date.appendChild(year);
Text tYear=doc.createTextNode("2003");
year.appendChild(tYear);
Element month=doc.createElement("month");
publish_date.appendChild(month);
Text tMonth=doc.createTextNode("05");
month.appendChild(tMonth);
Element day=doc.createElement("day");
publish_date.appendChild(day);
Text tDay=doc.createTextNode("29");
day.appendChild(tDay);
//创建描述
Element description=doc.createElement("description");
book.appendChild(description);
Text tDescription=doc.createTextNode("一本好书书书书");
description.appendChild(tDescription);
}
//写入XML文件
FileOutputStream output=new FileOutputStream("f:/book/books2.xml");
System.out.println("aa");
OutputStreamWriter outWriter=new OutputStreamWriter(output);
Object className=doc.getClass();
System.out.println("doc class name"+className);
if(doc instanceof XmlDocument){
System.out.println("cast is ok");
}
((XmlDocument) doc).write(outWriter, "GB2312");
outWriter.close();
output.close();
}catch(Exception e){
System.out.println(e);
}
}}
给你一段程序看看:File outFile = new File("InfoToAll.xml");
FileOutputStream outFileStream = new FileOutputStream(outFile);
PrintWriter outStream = new PrintWriter(outFileStream);
try
{
Element infoRoot = new Element("Information-Details");
infoRoot.addContent(new Element("Title").setText(title));
infoRoot.addContent(new Element("Content").setText(content)); Document infoDetails = new Document(infoRoot);
XMLOutputter outputter = new XMLOutputter(" ",true);
String InfoReturn = outputter.outputString(infoDetails);
outStream.println(InfoReturn);
outStream.close();
return InfoReturn;
}
catch(Exception e)
{
e.printStackTrace(System.out);
return "Error in InfoService.publishInfo(String title,String content)!";
}