- /**************************************
- * Title: 写入XML文件
- * Description: WriteXml类
- * date : 2006-3-25
- * author : zhanglei
- ***************************************/
- package server;
- import org.w3c.dom.*;
- import javax.xml.parsers.*;
- //import javax.xml.transform.*;
- //import javax.xml.transform.dom.DOMSource;
- //import javax.xml.transform.stream.StreamResult;
- import java.io.*;
- //import java.util.*;
- import org.apache.xml.serialize.*;
- public class WriteXml {
- private Element root = null;
- private Element root_1 = null;
- private Element root_2 = null;
- private Element root_new = null;
- private Document document;
- private String filename;
- public WriteXml() {
- };
- public void WriteFile(String name) throws ParserConfigurationException {
- try {
- filename = name;
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- document = builder.newDocument();
- }
- catch (Exception ex) {
- SaveLog.SetInfo("WriteFile1:" + ex, 2);
- }
- }
- public void AddRoot(int level, String rootName) {
- //创建根节点,创建根接点下level级节点
- try {
- if (level == 0) {
- root = document.createElement(rootName);
- document.appendChild(root);
- }
- else if (level == 1) {
- root_1 = document.createElement(rootName);
- root.appendChild(root_1);
- }
- else if (level == 2) {
- root_2 = document.createElement(rootName);
- root_1.appendChild(root_2);
- }
- }
- catch (Exception ex) {
- SaveLog.SetInfo("WriteFile中AddRoot:" + ex, 2);
- }
- }
- public void AddItem(int level, String itemName, String itemVal) {
- //建立节点下项目1及内容
- try {
- if (itemName == null) {
- itemName = "";
- }
- if (itemVal == null) {
- itemVal = "";
- }
- Element item = document.createElement(itemName);
- item.appendChild(document.createTextNode(itemVal));
- if (level == 2) {
- root_2.appendChild(item);
- }
- else if (level == 1) {
- root_1.appendChild(item);
- }
- else if (level == 0) {
- root.appendChild(item);
- }
- }
- catch (Exception ex) {
- SaveLog.SetInfo("WriteFile中AddItem1:" + ex, 2);
- }
- }
- public void AddItem(String itemName, String itemVal) {
- //建立下一节点项目1及内容
- try {
- if (itemName == null) {
- itemName = "";
- }
- if (itemVal == null) {
- itemVal = "";
- }
- Element item = document.createElement(itemName);
- item.appendChild(document.createTextNode(itemVal));
- root_new = item;
- }
- catch (Exception ex) {
- SaveLog.SetInfo("WriteFile中AddItem2:" + ex, 2);
- }
- }
- public void AddItem(int level, String itemName, String itemVal, String sxName[],
- String sxVal[]) {
- //建立节点下项目1及内容
- try {
- if (itemName == null) {
- itemName = "";
- }
- if (itemVal == null) {
- itemVal = "";
- }
- Element item = document.createElement(itemName);
- item.appendChild(document.createTextNode(itemVal));
- if (sxName != null && sxVal != null) {
- if (sxName.length > 0 && sxVal.length > 0 &
- sxVal.length == sxName.length) {
- for (int i = 0; i < sxName.length; i++) {
- item.setAttribute(sxName[i], sxVal[i]);
- }
- }
- }
- if (level == 0) {
- root.appendChild(item);
- }
- if (level == 1) {
- root_1.appendChild(item);
- }
- if (level == 2) {
- root_2.appendChild(item);
- }
- root_new = item;
- }
- catch (Exception ex) {
- SaveLog.SetInfo("WriteFile中AddItem3:" + ex, 2);
- }
- }
- public void AddItem(String itemName, String itemVal, String sxName[],
- String sxVal[]) {
- //建立上一节点下项目及内容
- try {
- if (itemName == null) {
- itemName = "";
- }
- if (itemVal == null) {
- itemVal = "";
- }
- Element item = document.createElement(itemName);
- item.appendChild(document.createTextNode(itemVal));
- if (sxName != null && sxVal != null) {
- if (sxName.length > 0 && sxVal.length > 0 &
- sxVal.length == sxName.length) {
- for (int i = 0; i < sxName.length; i++) {
- if (sxName[i].length() > 1) {
- item.setAttribute(sxName[i], sxVal[i]);
- }
- }
- }
- }
- root_new.appendChild(item);
- }
- catch (Exception ex) {
- SaveLog.SetInfo("WriteFile中AddItem4:" + ex, 2);
- }
- }
- /**
- *toSave 存储XML文件
- *@param flag 此XML文件头标志
- * flag = 0 为'UTF-8'标识
- * flag = 1 为"GB2312"标识
- * flag = 2 为"UTF-8",standalone="yes"标识
- * flag = 3 为"gb2312",standalone="yes"标识
- * flag = 4 为'UTF-16'标识
- */
- public void toSave(String flag) {
- try {
- //生成OutputFormat
- OutputFormat opf = new OutputFormat(document);
- opf.setVersion("1.0");
- if (flag.equals("1") || flag.equals("3")) {
- opf.setEncoding("gb2312"); //改成gb2312就行了UTF-8
- }
- else {
- opf.setEncoding("UTF-8"); //改成gb2312就行了UTF-8
- }
- if (flag.equals("2") || flag.equals("3")) {
- opf.setStandalone(true); //standalong
- }
- else {
- opf.setStandalone(false); //standalong
- }
- opf.setIndent(1); //标签是否加空格
- opf.setMediaType("application/xml");
- File myXML = new File(filename);
- FileOutputStream os = new FileOutputStream(myXML);
- XMLSerializer serializer = new XMLSerializer(os, opf);
- serializer.serialize(document);
- }
- catch (Exception ex) {
- SaveLog.SetInfo("WriteXml中toSave():" + ex, 2);
- }
- }
- public static void main(String args[]) {
- try {
- WriteXml myxml = new WriteXml();
- myxml.WriteFile("d:/b.xml");
- myxml.AddRoot(0, "Package");
- myxml.AddRoot(1, "PackageHead");
- String aa[] = new String[2];
- String bb[] = new String[2];
- aa[0] = "a1";
- bb[0] = "b1";
- aa[1] = "a2";
- bb[1] = "b2";
- myxml.AddRoot(1, "Data");
- myxml.AddItem(1, "item1", "新疆昌吉市长宁路132号锦绣江南小镇15幢3单元301号", aa, bb);
- myxml.AddRoot(2, "Data2");
- myxml.AddItem(2, "myitem", "sdfas");
- myxml.AddItem(2, "item1", "غەربى يولى شىمالى 2- مەھەللە", aa, bb);
- myxml.AddRoot(2, "Data3");
- myxml.AddRoot(1, "Data4");
- myxml.AddItem(1, "dfsd", "dfsd");
- myxml.toSave("0");
- System.out.print("Your writing is successful.");
- }
- catch (ParserConfigurationException exp) {
- exp.printStackTrace();
- System.out.print("Your writing is failed.");
- }
- //修改
- DocumentBuilder parser;
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- Document doc = null;
- Element root;
- Element begingItem;
- try {
- parser = factory.newDocumentBuilder();
- doc = parser.parse("links.xml");
- root = doc.getDocumentElement();
- NodeList items = root.getElementsByTagName("link");
- begingItem = (Element) items.item(0);
- NodeList names = begingItem.getElementsByTagName("url");
- Element editItem = (Element) names.item(0);
- editItem.setAttribute("newWindow", "yes");
- editItem.getFirstChild().setNodeValue("new觉得深刻法");
- Element title = doc.createElement("Title");
- title.appendChild(doc.createTextNode("mytitle"));
- root.appendChild(title);
- Element content = doc.createElement("Content");
- content.setAttribute("sx1", "sx1v");
- //content.removeAttribute("sx2");
- content.appendChild(doc.createTextNode("mycontent"));
- content.getFirstChild().setNodeValue("似的咖啡碱但是");
- root.appendChild(content);
- }
- catch (Exception e) {
- System.out.println(e);
- }
- try {
- //生成OutputFormat
- OutputFormat opf = new OutputFormat(doc);
- opf.setVersion("1.0");
- opf.setEncoding("gb2312"); //改成gb2312就行了UTF-8
- opf.setStandalone(false); //standalong
- opf.setIndent(1); //标签是否加空格
- opf.setMediaType("application/xml");
- File myXML = new File("user.xml");
- FileOutputStream os = new FileOutputStream(myXML);
- XMLSerializer serializer = new XMLSerializer(os, opf);
- serializer.serialize(doc);
- }
- catch (Exception ex) {
- System.out.println(ex);
- }
- }
- }
writexml.java
最新推荐文章于 2023-05-22 19:16:09 发布