SOAP向服务端提交datatable格式的数据解决方案

import java.io.File;

import java.io.FileWriter;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;

public class DataTableUtil {
public static void main(String[] args) {
getDataTableXML(“Tabel1”, new String[] { “dh”, “rwmc”, “rwks_date”, “rwjs_date”, “rwbz”, “rwzt”, “opt_user”, “opt_time”, “scbj”, “zt_bm”, “ztmc”, “yw_bm”, “ztmc1”, }, new String[][] { new String[] { “P20130719-001”, “test”, “19.07.13 9:44:53”, “19.07.13 9:44:53”, “”, “zcpd-03”, “系统管理员”, “19.07.13 9:45:55”, “0”, “zcpd-03”, “盘点中”, “zcpd”, “盘点中”, }, new String[] { “P20130719-001”, “test”, “19.07.13 9:44:53”, “19.07.13 9:44:53”, “”, “zcpd-03”, “系统管理员”, “19.07.13 9:45:55”, “0”, “zcpd-03”, “盘点中”, “zcpd”, “盘点中”, }, new String[] { “P20130719-001”, “test”, “19.07.13 9:44:53”, “19.07.13 9:44:53”, “”, “zcpd-03”, “系统管理员”, “19.07.13 9:45:55”, “0”, “zcpd-03”, “盘点中”, “zcpd”, “盘点中”, } });
}

public static String getDataTableXML(String tableName, String[] columnNames, String[][] datas) {
Document document = DocumentHelper.createDocument();
Element dataTable = document.addElement(“DataTable”);

// =====================================
// 开始封装数据库结构
// =====================================
// 组装schema
Element schema = DocumentHelper.createElement(“xs:schema”);
schema.addNamespace(“xs”, “http://www.w3.org/2001/XMLSchema”);
schema.addNamespace(“msdata”, “urn:schemas-microsoft-com:xml-msdata”);
schema.addAttribute(“id”, “NewDataSet”);
dataTable.add(schema);
// 组装scheme_element
// <xs:element name=”NewDataSet” msdata:IsDataSet=”true”
// msdata:MainDataTable=”testDT” msdata:UseCurrentLocale=”true”>
Element schemeElement = schema.addElement(“xs:element”);
schemeElement.addAttribute(“name”, “NewDataSet”);
schemeElement.addAttribute(“msdata:IsDataSet”, “true”);
schemeElement.addAttribute(“msdata:MainDataTable”, tableName);// TODO:
schemeElement.addAttribute(“msdata:UseCurrentLocale”, “true”);

Element schemeElementComplexType = schemeElement.addElement(“xs:complexType”);

// <xs:choice minOccurs=”0″ maxOccurs=”unbounded”>
Element schemeElementComplexTypeChoice = schemeElementComplexType.addElement(“xs:choice”);
schemeElementComplexTypeChoice.addAttribute(“minOccurs”, “0”);
schemeElementComplexTypeChoice.addAttribute(“maxOccurs”, “unbounded”);

// <xs:element name=”testDT”>
Element schemeElementComplexTypeChoiceElement = schemeElementComplexTypeChoice.addElement(“xs:element”);
schemeElementComplexTypeChoiceElement.addAttribute(“name”, tableName);// TODO:

// <xs:complexType>
Element schemeElementComplexTypeChoiceElementComplexType = schemeElementComplexTypeChoiceElement.addElement(“xs:complexType”);
// <xs:sequence>
Element schemeElementComplexTypeChoiceElementComplexTypeSequence = schemeElementComplexTypeChoiceElementComplexType.addElement(“xs:sequence”);
// <xs:element name=”dh” type=”xs:string” minOccurs=”0″/>
for (String string : columnNames) {
Element schemeElementComplexTypeChoiceElementComplexTypeSequenceElement = schemeElementComplexTypeChoiceElementComplexTypeSequence.addElement(“xs:element”);
schemeElementComplexTypeChoiceElementComplexTypeSequenceElement.addAttribute(“name”, string);// TODO:
schemeElementComplexTypeChoiceElementComplexTypeSequenceElement.addAttribute(“type”, “xs:string”);// TODO:
schemeElementComplexTypeChoiceElementComplexTypeSequenceElement.addAttribute(“minOccurs”, “0”);// TODO:
}

// =====================================
// 开始封装数据内容
// =====================================
// <diffgr:diffgram
// xmlns:diffgr=”urn:schemas-microsoft-com:xml-diffgram-v1″
// xmlns:msdata=”urn:schemas-microsoft-com:xml-msdata”>
Element diffgram = DocumentHelper.createElement(“diffgr:diffgram”);
diffgram.addNamespace(“diffgr”, “urn:schemas-microsoft-com:xml-diffgram-v1”);
diffgram.addNamespace(“msdata”, “urn:schemas-microsoft-com:xml-msdata”);
dataTable.add(diffgram);
// <DocumentElement>
Element documentElement = diffgram.addElement(“DocumentElement”);
for (int j = 0; j < datas.length; j++) {
Element tableNameNode = documentElement.addElement(tableName);
tableNameNode.addAttribute(“diffgr:id”, tableName);
tableNameNode.addAttribute(“msdata:rowOrder”, “0”);
for (int i = 0; i < datas[j].length; i++) {
Element dataNode = tableNameNode.addElement(columnNames[i]);
dataNode.addText(datas[j][i]);
}
}
// =====================================
// 输出XML内容
// =====================================
try {
XMLWriter output = new XMLWriter(new FileWriter(new File(“C:/Users/Administrator/Desktop/catalog.xml”)));
output.write(document);
output.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
return document.asXML();
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *