package com.hotel.utils; import java.io.IOException; import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; 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.dom4j.DocumentHelper; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** * * @作者:wanghaotao * @开发商:rojao * @日期:{2011-5-26 * @方法功能:{xml document 文件处理} * @修改 */ public class XMLDocument { private static String errorXml = "<?xml version=\"1.0\" encoding=\"gbk\" standalone=\"yes\"?><result><code>-1</code><message>封装xml出错</message></result>"; /** * {返回错误的xml} * * @param inMessage * @return * @author: wanghaotao */ public static String createErrorXml(String inMessage) { Document document = null; try { document = newDocument(); Element root = document.createElement("result"); document.appendChild(root); // 创建交易码标签 Element code = document.createElement("code"); code.appendChild(document.createTextNode("-1"));// 处理失败 // 创建消息标签 Element message = document.createElement("message"); message.appendChild(document.createTextNode(inMessage)); // 为根标签添加交易码和消息子标签 root.appendChild(code); root.appendChild(message); } catch (Exception e) { e.printStackTrace(); return errorXml; } // 创建根标签 return getStrFromDoc(document); } /** * {新创建一个Document 对象} * * @return * @throws Exception * @author: wanghaotao */ private static Document newDocument() throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null; try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e1) { e1.printStackTrace(); throw new Exception("创建xml builder 失败"); } return builder.newDocument(); } /** * {根据传入的document生成一个正式的xml document} * * @param docXml * @return * @author: wanghaotao */ public static String getStrFromDoc(Document docXml) { DOMSource domSource = new DOMSource(docXml); StringWriter out = new StringWriter(); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); try { Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(javax.xml.transform.OutputKeys.STANDALONE, "yes"); transformer.setOutputProperty(javax.xml.transform.OutputKeys.METHOD, "xml"); transformer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING, "gbk"); transformer.transform(domSource, streamResult); } catch (TransformerConfigurationException tce) { } catch (TransformerException te) { } catch (IllegalArgumentException ie) { } out.flush(); return out.toString(); /* * String str = new String(); System.out.println(new Date()); str = * pr_transform.TreeWalk(docXml); System.out.println(new Date()); return * str; */ } /** * * @作者:wanghaotao * @开发商:rojao * @日期:{2011-5-26 * @方法功能:{把xml文件转换为document * @param * @return */ private static Document getXMLDocFromStr(String strXml) { // 创建DocumentBuilder对象 DocumentBuilderFactory dbf = null; DocumentBuilder db = null; dbf = DocumentBuilderFactory.newInstance(); try { db = dbf.newDocumentBuilder(); } catch (ParserConfigurationException pce) { System.err.println(pce); return (null); } // 通过字符串得到Document对象 Document doc = null; Reader sr = new StringReader(strXml); InputSource is = new InputSource(sr); try { doc = db.parse(is); } catch (SAXException se) { doc = null; } catch (IOException ioe) { doc = null; } return doc; } /** * {通过文件路径得到文件内容:xml内容} * * @param FileSrc * @return * @throws Exception * @author: wanghaotao * @throws IOException * @throws SAXException */ public static String getXmlFromFile(String FileSrc) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db; Document doc = null; try { db = dbf.newDocumentBuilder(); doc = db.parse(FileSrc); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return getStrFromDoc(doc); } /** * * @作者:wanghaotao * @开发商:rojao * @日期:{ * @方法功能:{解析xml test} * @param * @return */ public static void main(String[] args) { /* * String xmlStr = * "<?xml version=\"1.0\" encoding=\"gbk\" standalone=\"no\"?>" + * "<input>" + "<name>丁宏亮</name>" + "<sex>m</sex>" + "<age>30</age>" + * "<dataset>" + "<row>" + "<akc220>aaaa</akc220>" + * "<ykc610>qqqq</ykc610>" + "</row>" + "<row>" + * "<akc220>aaaa1</akc220>" + "<ykc610>qqqq1</ykc610>" + "</row>" + * "</dataset>" + "</input>"; try { HashMap hs = * getAllParasFromDoc(xmlStr); Iterator iter = hs.entrySet().iterator(); * while (iter.hasNext()) { Map.Entry e = (Map.Entry) iter.next(); * System.out.print(e.getKey() + " is "); * System.out.println(e.getValue()); } List list = (List) * hs.get("dataset"); if (list == null) return; FeeDetail feeDetail; for * (int i = 0; i < list.size(); i++) { feeDetail = (FeeDetail) * list.get(i); System.out.println(feeDetail.getAkc220()); * System.out.println(feeDetail.getYkc610()); } * * } catch (Exception e) { * e.printStackTrace(); } */ String path = System.getProperty("user.dir") + "/time.xml"; String xmlStr = getXmlFromFile(path); System.out.println(xmlStr); // System.out.println("sdfsd"); } }
最近下载更多
annazhang LV29
2021年9月25日
tanyc2008 LV6
2020年12月1日
xiehao1020 LV1
2020年3月5日
李慧都 LV2
2019年1月24日
122374655 LV1
2017年3月27日
zouquan1027 LV2
2016年5月22日
zhangleisz LV1
2016年2月17日
zw5097 LV23
2016年2月15日
George LV26
2014年3月1日
最近浏览更多
冰糖雪里 LV3
2023年11月10日
qingfengqingzhou LV1
2022年4月22日
JSPMVC
2021年12月9日
暂无贡献等级
annazhang LV29
2021年9月25日
dengkaixing LV2
2021年5月26日
tanyc2008 LV6
2020年12月1日
152162355 LV3
2020年5月25日
lsq54365 LV14
2020年5月20日
qianyunjie LV8
2020年4月14日
送快递俊 LV3
2020年4月14日