Java创建xml与排序
DOM创建xml
package com.lzb.model.xml;import java.io.File;import java.io.FileOutputStream;import java.util.Map;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.transform.OutputKeys;import javax.xml.transform.Transformer;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;/** * 测试采用DOM的方式生成XML * @author lizhenbin * */public class DomCreateXml {/** * 参考 * @param obj * @param path * @return */public static boolean domCreateXmlDoc() {try {/** * 初始化 */Document doc = null;DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();DocumentBuilder docBuilder = dbfactory.newDocumentBuilder();doc = docBuilder.newDocument();/** * 创建XML的根节点(根) */Element root = doc.createElement("xml_root"); //XML的根节点doc.appendChild(root);for(int i=0; i<3; i++) {String _s = "child_" + i;Element _child = doc.createElement(_s);root.appendChild(_child);for(int j=0; j<2; j++) {String _s_s = _s + "_" + j;Element _c_child = doc.createElement(_s_s);_c_child.appendChild(doc.createTextNode("城市编码: SN" + _s_s));_child.appendChild(_c_child);}}/** * 结束拼凑XML */TransformerFactory tff = TransformerFactory.newInstance();Transformer transformer = tff.newTransformer();/** * xml输出的位置 */File file = new File("E://testcreate.xml");FileOutputStream out = new FileOutputStream(file);StreamResult xmlResult = new StreamResult(out);transformer.setOutputProperty(OutputKeys.INDENT, "yes");transformer.transform(new DOMSource(doc), xmlResult);return true;} catch (Exception e) {e.printStackTrace();return false;}}}
?
SAX创建xml
package com.lzb.model.xml;import java.io.File;import java.io.FileOutputStream;import java.io.StringWriter;import javax.xml.transform.OutputKeys;import javax.xml.transform.Result;import javax.xml.transform.Transformer;import javax.xml.transform.sax.SAXTransformerFactory;import javax.xml.transform.sax.TransformerHandler;import javax.xml.transform.stream.StreamResult;import org.xml.sax.helpers.AttributesImpl;/** * 测试才用SAX的方式生成XML * @author lizhenbin * */public class SaxCreateXml {/** * * 功能描述: * * @author lizhenbin * <p>创建日期 :May 8, 2012 7:09:13 PM</p> * * @throws Exception * * <p>修改历史 :(修改人,修改时间,修改原因/内容)</p> */public static void saxCreateXmlDoc() throws Exception {String xmlStr = null;//String dstPath = ServletActionContext.getServletContext().getRealPath(this.getSavePath()) + "\" + this.getUploadFileName();Result resultXml = new StreamResult(new FileOutputStream("E://sax_xml.xml"));StringWriter writerStr = new StringWriter();SAXTransformerFactory sff = (SAXTransformerFactory) SAXTransformerFactory.newInstance();TransformerHandler th = sff.newTransformerHandler();Transformer transformer = th.getTransformer();transformer.setOutputProperty(OutputKeys.INDENT, "yes");transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");th.setResult(resultXml);/** * 拼凑xml, SAX的方式,每个都有开启和结束配对 */th.startDocument();AttributesImpl attr = new AttributesImpl();th.startElement("", "", "root", attr); //根节点for(int i=0; i<2; i++) {String _c = "child_" + i;th.startElement("", "", _c, attr);for(int j=0; j<3; j++) {String _c_c = _c + "_" +j;th.startElement("", "", _c_c, attr);for(int k=0; k<2; k++) {String _c_c_c = _c_c + "_" + k;String txt = "城市编号:" + _c_c_c;th.startElement("", "", _c_c_c, attr);th.characters(txt.toCharArray(), 0, txt.length());th.endElement("", "", _c_c_c);}th.endElement("", "", _c_c);}th.endElement("", "", _c);}th.endElement("", "", "root");th.endDocument();//th.startDocument();//AttributesImpl attr = new AttributesImpl();//th.startElement("", "", "xmlroot", attr); //根节点//th.startElement("", "", "child", attr); //孩子////th.startElement("", "", "context", attr); //内容//String content1 = "北京";//th.characters(content1.toCharArray(), 0, content1.length());//th.endElement("", "", "context");////th.startElement("", "", "context", attr); //内容//String content2 = "上海";//th.characters(content2.toCharArray(), 0, content2.length());//th.endElement("", "", "context");////th.endElement("", "", "child");//th.endElement("", "", "xmlroot");//th.endDocument();/** * 结束 */xmlStr = writerStr.getBuffer().toString();//return xmlStr;}}
?
最后补充一下排序算法,主要是因为我创建xml的时候,想自己写个方法排序
package com.lzb.model.sort;/** * * 功能描述:排序 * * @author Administrator * * <p>修改历史:(修改人,修改时间,修改原因/内容)</p> */public class SortAlgorithm {/** * * 功能描述:冒泡排序算法(稳定) * 假设有N个数据需要排序,则从第0个数开始,依次比较第0和第1个数据,如果第0个大于第1个则两者交换,否则什么动作都不做,继续比较第1个第2个…,这样依次类推,直至所有数据都“冒泡”到数据顶上。 * * @author Administrator * <p>创建日期 :2012-3-16 下午4:27:20</p> * * * <p>修改历史 :(修改人,修改时间,修改原因/内容)</p> */public void bubbleSort(int[] targets) {boolean flag;for(int i=0; i<targets.length-1; i++) {flag = true;for(int j=0; j<targets.length-i-1; j++) {if(targets[j] > targets[j+1]) {int temp = targets[j];targets[j] = targets[j+1];targets[j+1] = temp;flag = false;}}if(flag==true)break;}for(Integer i : targets) {System.out.print(i + " ");}}/** * * 功能描述:选择排序算法(不稳定) * 假设有N条数据,则暂且标记第0个数据为MIN(最小),使用OUT标记最左边未排序的数据,然后使用IN标记第1个数据,依次与MIN进行比较,如果比MIN小,则将该数据标记为MIN,当第一轮比较完后,最终的MIN与OUT标记数据交换,依次类推; * * @author Administrator * <p>创建日期 :2012-3-16 下午5:21:23</p> * * * <p>修改历史 :(修改人,修改时间,修改原因/内容)</p> */public void selectSort(int[] targets) {boolean flag;for(int i=0; i<targets.length-1; i++) {flag = true;for(int j=i+1; j<targets.length; j++) {if(targets[i] > targets[j]) {int temp = targets[j];targets[j] = targets[i];targets[i] = temp;flag = false;}}if(flag==true)break;}for(Integer i : targets) {System.out.print(i + " ");}}/*/** * * 功能描述:插入排序算法(稳定) * 插入排序是在部分数据有序的情况下,使用OUT标记第一个无序的数据,将其提取保存到一个中间变量temp中去,使用IN标记空位置,依次比较temp中的值与IN‐1的值,如果IN‐值大于temp的值,则后移,直到遇到第一个比temp小的值,在其下一个位置插入; * * @author Administrator * <p>创建日期 :2012-3-18 下午2:27:16</p> * * @param targets * * <p>修改历史 :(修改人,修改时间,修改原因/内容)</p> */public void InsertionSort(int[] targets) {}*/public static void main(String[] args) {SortAlgorithm sort = new SortAlgorithm();int[] _istore = {4,2,3,0,1,3,-2,10};sort.selectSort(_istore);System.out.println("\n--------------------------");sort.bubbleSort(_istore);}}
?