package hygd.grjz.exchange.utils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.Date;
import java.util.UUID;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class XmlUtil {
public String createXML(DGParam param, String filename) {
String xmlStr = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
document.setXmlVersion("1.0");
document.setXmlStandalone(true);
Element Request = document.createElement("Request");
document.appendChild(Request);
Element Ctrl = document.createElement("Ctrl");
Element eciSeverId = document.createElement("eciSeverId");
eciSeverId.setTextContent(param.getEciSeverId());
Ctrl.appendChild(eciSeverId);
Element xmlflag = document.createElement("xmlflag");
xmlflag.setTextContent(param.getXmlflag());
Ctrl.appendChild(xmlflag);
Element templateCodeName = document.createElement("templateCodeName");
templateCodeName.setTextContent(param.getTemplateCodeName());
Ctrl.appendChild(templateCodeName);
Element transCode = document.createElement("transCode");
transCode.setTextContent(param.getTransCode());
Ctrl.appendChild(transCode);
Element sysId = document.createElement("sysId");
sysId.setTextContent(param.getSysId());
Ctrl.appendChild(sysId);
Element channelCode = document.createElement("channelCode");
channelCode.setTextContent(param.getChannelCode());
Ctrl.appendChild(channelCode);
Element subchannelCode = document.createElement("subchannelCode");
subchannelCode.setTextContent(param.getSubchannelCode());
Ctrl.appendChild(subchannelCode);
Element tradeFlag = document.createElement("tradeFlag");
tradeFlag.setTextContent("0");
Ctrl.appendChild(tradeFlag);
Element checkFlag = document.createElement("checkFlag");
checkFlag.setTextContent("0");
Ctrl.appendChild(checkFlag);
Element channelserno = document.createElement("channelserno");
String s = UUID.randomUUID().toString();
// 唯一流水
s = s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18);
long ts = new Date().getTime();
s = s + String.valueOf(ts);
channelserno.setTextContent(s);
Ctrl.appendChild(channelserno);
Element sessid = document.createElement("sessid");
sessid.setTextContent("");
Ctrl.appendChild(sessid);
Element prcscd = document.createElement("prcscd");
prcscd.setTextContent(param.getPrcscd());
Ctrl.appendChild(prcscd);
Element zoneno = document.createElement("zoneno");
zoneno.setTextContent("");
Ctrl.appendChild(zoneno);
Element mbrno = document.createElement("mbrno");
mbrno.setTextContent("");
Ctrl.appendChild(mbrno);
Element brno = document.createElement("brno");
brno.setTextContent("");
Ctrl.appendChild(brno);
Element tellerno = document.createElement("tellerno");
tellerno.setTextContent("");
Ctrl.appendChild(tellerno);
Element tellertp = document.createElement("tellertp");
tellertp.setTextContent("");
Ctrl.appendChild(tellertp);
Element csbxno = document.createElement("csbxno");
csbxno.setTextContent("");
Ctrl.appendChild(csbxno);
Element dutytp = document.createElement("dutytp");
dutytp.setTextContent("");
Ctrl.appendChild(dutytp);
Element dutyno = document.createElement("dutyno");
dutyno.setTextContent("");
Ctrl.appendChild(dutyno);
Element authno = document.createElement("authno");
authno.setTextContent("");
Ctrl.appendChild(authno);
Element authpw = document.createElement("authpw");
authpw.setTextContent("");
Ctrl.appendChild(authpw);
Element authmg = document.createElement("authmg");
authmg.setTextContent("");
Ctrl.appendChild(authmg);
Element authce = document.createElement("authce");
authce.setTextContent("");
Ctrl.appendChild(authce);
Element authmanfttype = document.createElement("authmanfttype");
authmanfttype.setTextContent("");
Ctrl.appendChild(authmanfttype);
Request.appendChild(Ctrl);
Element Body = document.createElement("Body");
Element Eciorgbuf = document.createElement("Eciorgbuf");
Eciorgbuf.setTextContent("");
Body.appendChild(Eciorgbuf);
Element filena = document.createElement("filena");
filena.setTextContent(filename);
Body.appendChild(filena);
Element trantp = document.createElement("trantp");
trantp.setTextContent(param.getTrantp());
Body.appendChild(trantp);
Request.appendChild(Body);
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transFormer = transFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
// export string
ByteArrayOutputStream bos = new ByteArrayOutputStream();
transFormer.transform(domSource, new StreamResult(bos));
xmlStr = bos.toString();
int length = xmlStr.length();
String lenString = String.format("%08d", length);
String fileString = lenString + xmlStr;
System.out.println(fileString);
// -------
// save as file
File file = new File(param.getXmlPath());
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file);
out.write(fileString.getBytes());
out.flush();
out.close();
// StreamResult xmlResult = new StreamResult(out);
// transFormer.transform(domSource, xmlResult);
// --------
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return xmlStr;
}
public void parserXML(String strXML) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
StringReader sr = new StringReader(strXML);
InputSource is = new InputSource(sr);
Document doc = builder.parse(is);
Element rootElement = doc.getDocumentElement();
NodeList phones = rootElement.getElementsByTagName("type");
for (int i = 0; i < phones.getLength(); i++) {
Node type = phones.item(i);
String phoneName = ((Element) type).getAttribute("name");
System.out.println("Phone name = " + phoneName);
NodeList properties = type.getChildNodes();
for (int j = 0; j < properties.getLength(); j++) {
Node property = properties.item(j);
String nodeName = property.getNodeName();
if (nodeName.equals("price")) {
String price = property.getFirstChild().getNodeValue();
System.out.println("price=" + price);
} else if (nodeName.equals("operator")) {
String operator = property.getFirstChild().getNodeValue();
System.out.println("operator=" + operator);
}
}
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
// DGParam param = new DGParam();
// XmlUtil util = new XmlUtil();
// util.createXML(param, "C:\\temp\\TelePhone.xml");
// int i = 700;
// String string = String.format("%08d", i);
// System.out.println(string);
// File file = new File("C:\\temp\\TelePhone.xml");
// try {
// FileOutputStream out = new FileOutputStream(file);
// String string =
// "00007000<Request><Ctrl><eciSeverId>xxxxxxxxxx</eciSeverId>";
// out.write(string.getBytes());
// } catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
String s = UUID.randomUUID().toString();
// 去掉“-”符号
s = "C:\\temp\\zip\\C1104412000011cams001001012017041013501585881.zip";
// long ts = new Date().getTime();
s = s.replace("zip", "dat");
System.out.println(s);
// File zipFile = new File("C:\\temp\\TelePhone.xml");
// String zipfilename = zipFile.getName();
// // String newzipfilename = zipfilename.substring(42);
// File newzipfile = new File("C:\\temp\\TelePhone2.xml");
// zipFile.renameTo(newzipfile);
}
}
00000628<?xml version="1.0" encoding="UTF-8"?><Request><Ctrl><eciSeverId>1</eciSeverId><xmlflag>2</xmlflag><templateCodeName>3</templateCodeName><transCode>4</transCode><sysId>5</sysId><channelCode>6</channelCode><subchannelCode>03.7</subchannelCode><tradeFlag>0</tradeFlag><checkFlag>0</checkFlag><channelserno>461ccc494e074f541491889831468</channelserno><sessid/><prcscd>11</prcscd><zoneno/><mbrno/><brno/><tellerno/><tellertp/><csbxno/><dutytp/><dutyno/><authno/><authpw/><authmg/><authce/><authmanfttype/></Ctrl><Body><Eciorgbuf/><filena>C1104412000011cams001001012017041113502193719.zip</filena><trantp>14</trantp></Body></Request>