writexml.java

来源:互联网 发布:php微信商城开发源码 编辑:程序博客网 时间:2024/05/17 04:57
  1. /**************************************
  2.  * Title:        写入XML文件
  3.  * Description:  WriteXml类
  4.  * date :        2006-3-25
  5.  * author :      zhanglei
  6.  ***************************************/
  7. package server;
  8. import org.w3c.dom.*;
  9. import javax.xml.parsers.*;
  10. //import javax.xml.transform.*;
  11. //import javax.xml.transform.dom.DOMSource;
  12. //import javax.xml.transform.stream.StreamResult;
  13. import java.io.*;
  14. //import java.util.*;
  15. import org.apache.xml.serialize.*;
  16. public class WriteXml {
  17.     private Element root = null;
  18.     private Element root_1 = null;
  19.     private Element root_2 = null;
  20.     private Element root_new = null;
  21.     private Document document;
  22.     private String filename;
  23.     public WriteXml() {
  24.     };
  25.     public void WriteFile(String name) throws ParserConfigurationException {
  26.         try {
  27.             filename = name;
  28.             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  29.             DocumentBuilder builder = factory.newDocumentBuilder();
  30.             document = builder.newDocument();
  31.         }
  32.         catch (Exception ex) {
  33.             SaveLog.SetInfo("WriteFile1:" + ex, 2);
  34.         }
  35.     }
  36.     public void AddRoot(int level, String rootName) {
  37.         //创建根节点,创建根接点下level级节点
  38.         try {
  39.             if (level == 0) {
  40.                 root = document.createElement(rootName);
  41.                 document.appendChild(root);
  42.             }
  43.             else if (level == 1) {
  44.                 root_1 = document.createElement(rootName);
  45.                 root.appendChild(root_1);
  46.             }
  47.             else if (level == 2) {
  48.                 root_2 = document.createElement(rootName);
  49.                 root_1.appendChild(root_2);
  50.             }
  51.         }
  52.         catch (Exception ex) {
  53.             SaveLog.SetInfo("WriteFile中AddRoot:" + ex, 2);
  54.         }
  55.     }
  56.     public void AddItem(int level, String itemName, String itemVal) {
  57.         //建立节点下项目1及内容
  58.         try {
  59.             if (itemName == null) {
  60.                 itemName = "";
  61.             }
  62.             if (itemVal == null) {
  63.                 itemVal = "";
  64.             }
  65.             Element item = document.createElement(itemName);
  66.             item.appendChild(document.createTextNode(itemVal));
  67.             if (level == 2) {
  68.                 root_2.appendChild(item);
  69.             }
  70.             else if (level == 1) {
  71.                 root_1.appendChild(item);
  72.             }
  73.             else if (level == 0) {
  74.                 root.appendChild(item);
  75.             }
  76.         }
  77.         catch (Exception ex) {
  78.             SaveLog.SetInfo("WriteFile中AddItem1:" + ex, 2);
  79.         }
  80.     }
  81.     public void AddItem(String itemName, String itemVal) {
  82.         //建立下一节点项目1及内容
  83.         try {
  84.             if (itemName == null) {
  85.                 itemName = "";
  86.             }
  87.             if (itemVal == null) {
  88.                 itemVal = "";
  89.             }
  90.             Element item = document.createElement(itemName);
  91.             item.appendChild(document.createTextNode(itemVal));
  92.             root_new = item;
  93.         }
  94.         catch (Exception ex) {
  95.             SaveLog.SetInfo("WriteFile中AddItem2:" + ex, 2);
  96.         }
  97.     }
  98.     public void AddItem(int level, String itemName, String itemVal, String sxName[],
  99.                         String sxVal[]) {
  100.         //建立节点下项目1及内容
  101.         try {
  102.             if (itemName == null) {
  103.                 itemName = "";
  104.             }
  105.             if (itemVal == null) {
  106.                 itemVal = "";
  107.             }
  108.             Element item = document.createElement(itemName);
  109.             item.appendChild(document.createTextNode(itemVal));
  110.             if (sxName != null && sxVal != null) {
  111.                 if (sxName.length > 0 && sxVal.length > 0 &
  112.                     sxVal.length == sxName.length) {
  113.                     for (int i = 0; i < sxName.length; i++) {
  114.                         item.setAttribute(sxName[i], sxVal[i]);
  115.                     }
  116.                 }
  117.             }
  118.             if (level == 0) {
  119.                 root.appendChild(item);
  120.             }
  121.             if (level == 1) {
  122.                 root_1.appendChild(item);
  123.             }
  124.             if (level == 2) {
  125.                 root_2.appendChild(item);
  126.             }
  127.             root_new = item;
  128.         }
  129.         catch (Exception ex) {
  130.             SaveLog.SetInfo("WriteFile中AddItem3:" + ex, 2);
  131.         }
  132.     }
  133.     public void AddItem(String itemName, String itemVal, String sxName[],
  134.                         String sxVal[]) {
  135.         //建立上一节点下项目及内容
  136.         try {
  137.             if (itemName == null) {
  138.                 itemName = "";
  139.             }
  140.             if (itemVal == null) {
  141.                 itemVal = "";
  142.             }
  143.             Element item = document.createElement(itemName);
  144.             item.appendChild(document.createTextNode(itemVal));
  145.             if (sxName != null && sxVal != null) {
  146.                 if (sxName.length > 0 && sxVal.length > 0 &
  147.                     sxVal.length == sxName.length) {
  148.                     for (int i = 0; i < sxName.length; i++) {
  149.                         if (sxName[i].length() > 1) {
  150.                             item.setAttribute(sxName[i], sxVal[i]);
  151.                         }
  152.                     }
  153.                 }
  154.             }
  155.             root_new.appendChild(item);
  156.         }
  157.         catch (Exception ex) {
  158.             SaveLog.SetInfo("WriteFile中AddItem4:" + ex, 2);
  159.         }
  160.     }
  161.     /**
  162.      *toSave            存储XML文件
  163.      *@param flag       此XML文件头标志
  164.      *           flag = 0 为'UTF-8'标识
  165.      *           flag = 1 为"GB2312"标识
  166.      *           flag = 2 为"UTF-8",standalone="yes"标识
  167.      *           flag = 3 为"gb2312",standalone="yes"标识
  168.      *       flag = 4 为'UTF-16'标识
  169.      */
  170.     public void toSave(String flag) {
  171.         try {
  172.             //生成OutputFormat
  173.             OutputFormat opf = new OutputFormat(document);
  174.             opf.setVersion("1.0");
  175.             if (flag.equals("1") || flag.equals("3")) {
  176.                 opf.setEncoding("gb2312"); //改成gb2312就行了UTF-8
  177.             }
  178.             else {
  179.                 opf.setEncoding("UTF-8"); //改成gb2312就行了UTF-8
  180.             }
  181.             if (flag.equals("2") || flag.equals("3")) {
  182.                 opf.setStandalone(true); //standalong
  183.             }
  184.             else {
  185.                 opf.setStandalone(false); //standalong
  186.             }
  187.             opf.setIndent(1); //标签是否加空格
  188.             opf.setMediaType("application/xml");
  189.             File myXML = new File(filename);
  190.             FileOutputStream os = new FileOutputStream(myXML);
  191.             XMLSerializer serializer = new XMLSerializer(os, opf);
  192.             serializer.serialize(document);
  193.         }
  194.         catch (Exception ex) {
  195.             SaveLog.SetInfo("WriteXml中toSave():" + ex, 2);
  196.         }
  197.     }
  198.     public static void main(String args[]) {
  199.         try {
  200.             WriteXml myxml = new WriteXml();
  201.             myxml.WriteFile("d:/b.xml");
  202.             myxml.AddRoot(0"Package");
  203.             myxml.AddRoot(1"PackageHead");
  204.             String aa[] = new String[2];
  205.             String bb[] = new String[2];
  206.             aa[0] = "a1";
  207.             bb[0] = "b1";
  208.             aa[1] = "a2";
  209.             bb[1] = "b2";
  210.             myxml.AddRoot(1"Data");
  211.             myxml.AddItem(1"item1""新疆昌吉市长宁路132号锦绣江南小镇15幢3单元301号", aa, bb);
  212.             myxml.AddRoot(2"Data2");
  213.             myxml.AddItem(2"myitem""sdfas");
  214.             myxml.AddItem(2"item1""غەربى يولى شىمالى 2- مەھەللە", aa, bb);
  215.             myxml.AddRoot(2"Data3");
  216.             myxml.AddRoot(1"Data4");
  217.             myxml.AddItem(1"dfsd""dfsd");
  218.             myxml.toSave("0");
  219.             System.out.print("Your writing is successful.");
  220.         }
  221.         catch (ParserConfigurationException exp) {
  222.             exp.printStackTrace();
  223.             System.out.print("Your writing is failed.");
  224.         }
  225.         //修改
  226.         DocumentBuilder parser;
  227.         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  228.         Document doc = null;
  229.         Element root;
  230.         Element begingItem;
  231.         try {
  232.             parser = factory.newDocumentBuilder();
  233.             doc = parser.parse("links.xml");
  234.             root = doc.getDocumentElement();
  235.             NodeList items = root.getElementsByTagName("link");
  236.             begingItem = (Element) items.item(0);
  237.             NodeList names = begingItem.getElementsByTagName("url");
  238.             Element editItem = (Element) names.item(0);
  239.             editItem.setAttribute("newWindow""yes");
  240.             editItem.getFirstChild().setNodeValue("new觉得深刻法");
  241.             Element title = doc.createElement("Title");
  242.             title.appendChild(doc.createTextNode("mytitle"));
  243.             root.appendChild(title);
  244.             Element content = doc.createElement("Content");
  245.             content.setAttribute("sx1""sx1v");
  246.             //content.removeAttribute("sx2");
  247.             content.appendChild(doc.createTextNode("mycontent"));
  248.             content.getFirstChild().setNodeValue("似的咖啡碱但是");
  249.             root.appendChild(content);
  250.         }
  251.         catch (Exception e) {
  252.             System.out.println(e);
  253.         }
  254.         try {
  255.             //生成OutputFormat
  256.             OutputFormat opf = new OutputFormat(doc);
  257.             opf.setVersion("1.0");
  258.             opf.setEncoding("gb2312"); //改成gb2312就行了UTF-8
  259.             opf.setStandalone(false); //standalong
  260.             opf.setIndent(1); //标签是否加空格
  261.             opf.setMediaType("application/xml");
  262.             File myXML = new File("user.xml");
  263.             FileOutputStream os = new FileOutputStream(myXML);
  264.             XMLSerializer serializer = new XMLSerializer(os, opf);
  265.             serializer.serialize(doc);
  266.         }
  267.         catch (Exception ex) {
  268.             System.out.println(ex);
  269.         }
  270.     }
  271. }
 
原创粉丝点击