用Applet读写XML配置文件

发布于 2022-09-01 10:19:29 字数 9648 浏览 17 评论 0

  1. 此小程序用DOM来读取XML文件,用DOM4j来写XML文件。
  2. DOM4j下载地址:http://sourceforge.net/projects/dom4j
  3. 1、Applet主程序mainFrame.java
  4. package frame;
  5. /**
  6. * @author wls
  7. */
  8. import java.applet.*;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. public class mainFrame
  12. extends Applet {
  13. DomXml domxml = new DomXml();
  14. Button btnExit = new Button("Exit");
  15. Button btnRun = new Button("Save");
  16. Label lblMain = new Label("dynamicroute.xml配置文件");
  17. Label lblSiteinfo = new Label("srcserverid:");
  18. Label lblPath = new Label("destserverid:");
  19. Label lblFilename = new Label("switchserver:");
  20. Label lblSession = new Label("metric:");
  21. Label lblSuccess = new Label(" ");
  22. TextField txtSiteinfo = new TextField(domxml.getSrcserverid(), 10);
  23. TextField txtPath = new TextField(domxml.getDestserverid(), 10);
  24. TextField txtFilename = new TextField(domxml.getSwitchserver(), 10);
  25. TextField txtSession = new TextField(domxml.getMetric(), 10);
  26. Panel p6 = new Panel();
  27. Panel p1 = new Panel();
  28. Panel p2 = new Panel();
  29. Panel p3 = new Panel();
  30. Panel p4 = new Panel();
  31. Panel p5 = new Panel();
  32. Panel p7 = new Panel();
  33. Panel p8 = new Panel();
  34. public void init() {
  35. resize(400, 300);
  36. p6.add(lblMain);
  37. p1.add(lblSiteinfo);
  38. p1.add(txtSiteinfo);
  39. p2.add(lblPath);
  40. p2.add(txtPath);
  41. p3.add(lblSession);
  42. p3.add(txtSession);
  43. p4.add(btnRun);
  44. p4.add(btnExit);
  45. p5.add(lblFilename);
  46. p5.add(txtFilename);
  47. p8.add(lblSuccess);
  48. setLayout(new GridLayout(4, 2, 1, 1));
  49. add(p6);
  50. add(p7);
  51. add(p1);
  52. add(p2);
  53. add(p5);
  54. add(p3);
  55. add(p4);
  56. add(p8);
  57. }
  58. public boolean action(Event evt, Object obj) {
  59. if (evt.target instanceof Button) {
  60. String btnName = (String) obj;
  61. if (btnName.equals("Save")) {
  62. try {
  63. domxml.setSrcserverid(txtSiteinfo.getText());
  64. domxml.setDestserverid(txtPath.getText());
  65. domxml.setSwitchserver(txtFilename.getText());
  66. domxml.setMetric(txtSession.getText());
  67. buildXml buildxml = new buildXml();
  68. if (buildxml.createXMLFile("dynamicroute.xml", domxml) == 1 &&
  69. buildxml.formatXMLFile("dynamicroute.xml")==1) {
  70. lblSuccess.setText("Save success!");
  71. System.out.println("Save success!");
  72. }
  73. }
  74. catch (Exception e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. else if (btnName.equals("Exit")) {
  79. System.exit(0);
  80. }
  81. }
  82. return super.action(evt, obj);
  83. }
  84. public static void main(String args[]) {
  85. new FrameDownload(new mainFrame());
  86. }
  87. }
  88. class FrameDownload
  89. extends Frame {
  90. public FrameDownload(Applet applet) {
  91. setSize(400, 300);
  92. add("Center", applet);
  93. applet.init();
  94. setVisible(true);
  95. this.addWindowListener(new closeWindows());
  96. }
  97. }
  98. class closeWindows
  99. extends WindowAdapter {
  100. public void windowClosing(WindowEvent e) {
  101. Frame frm = (Frame) (e.getSource());
  102. frm.dispose();
  103. System.exit(0);
  104. }
  105. }
  106. 2、读取XML类DomXml.java
  107. package frame;
  108. import javax.xml.parsers.*;
  109. import org.w3c.dom.*;
  110. /*
  111. * Created on 2004-8-19
  112. * @author wls
  113. *java读取XML文档
  114. *利用DoM来读取一个XML文档的内容
  115. *
  116. */
  117. public class DomXml {
  118. private String srcserverid;
  119. private String destserverid;
  120. private String switchserver;
  121. private String metric;
  122. public DomXml() {
  123. try {
  124. initXml("dynamicroute.xml");
  125. }
  126. catch (Exception ex) {
  127. metric="0";
  128. }
  129. }
  130. public String getSrcserverid() {
  131. return srcserverid;
  132. }
  133. public void setSrcserverid(String srcserverid) {
  134. this.srcserverid = srcserverid;
  135. }
  136. public String getDestserverid() {
  137. return destserverid;
  138. }
  139. public void setDestserverid(String destserverid) {
  140. this.destserverid = destserverid;
  141. }
  142. public String getSwitchserver() {
  143. return switchserver;
  144. }
  145. public void setSwitchserver(String switchserver) {
  146. this.switchserver = switchserver;
  147. }
  148. public String getMetric() {
  149. return metric;
  150. }
  151. public void setMetric(String metric) {
  152. this.metric = metric;
  153. }
  154. public void initXml(String fileName) throws Exception {
  155. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  156. DocumentBuilder bulider = factory.newDocumentBuilder();
  157. Document doc = bulider.parse(fileName);
  158. NodeList nl = doc.getElementsByTagName("dynamicroute");
  159. for (int i = 0; i < nl.getLength(); i++) {
  160. Element node = (Element) nl.item(i);
  161. srcserverid = node.getElementsByTagName("srcserverid").item(0).
  162. getFirstChild().getNodeValue().trim();
  163. destserverid = node.getElementsByTagName("destserverid").item(0).
  164. getFirstChild().getNodeValue().trim();
  165. switchserver = node.getElementsByTagName("serverid").item(0).
  166. getFirstChild().getNodeValue().trim();
  167. metric = node.getElementsByTagName("metric").item(0).getFirstChild().
  168. getNodeValue().trim();
  169. }
  170. }
  171. }
  172. 3、写XML文件类buildXml.java
  173. package frame;
  174. import java.io.*;
  175. import org.dom4j.*;
  176. import org.dom4j.io.*;
  177. /**
  178. * @author wls
  179. */
  180. public class buildXml {
  181. public buildXml() {
  182. }
  183. /**
  184. * 建立一个XML文档,文档名由输入参数决定
  185. * @param filename 需建立的文件名
  186. * @return 返回操作结果, 0表失败, 1表成功
  187. */
  188. public int createXMLFile(String filename, DomXml domxml) {
  189. /** 返回操作结果, 0表失败, 1表成功 */
  190. int returnValue = 0;
  191. /** 建立document对象 */
  192. Document document = DocumentHelper.createDocument();
  193. /** 建立XML文档的根route */
  194. Element routeElement = document.addElement("route");
  195. /** 加入第一个dynamicroute节点 */
  196. Element dynamicrouteElement = routeElement.addElement("dynamicroute");
  197. /** 加入show参数内容 */
  198. //bookElement.addAttribute("show","yes");
  199. /** 加入节点 */
  200. Element srcserveridElement = dynamicrouteElement.addElement("srcserverid");
  201. Element destserveridElement = dynamicrouteElement.addElement("destserverid");
  202. Element switchserverElement = dynamicrouteElement.addElement("switchserver");
  203. Element serveridElement = switchserverElement.addElement("serverid");
  204. Element metricElement = dynamicrouteElement.addElement("metric");
  205. /** 为title设置内容 */
  206. srcserveridElement.setText(domxml.getSrcserverid());
  207. destserveridElement.setText(domxml.getDestserverid());
  208. serveridElement.setText(domxml.getSwitchserver());
  209. metricElement.setText(domxml.getMetric());
  210. try {
  211. /** 将document中的内容写入文件中 */
  212. XMLWriter writer = new XMLWriter(new FileWriter(new File(filename)));
  213. writer.write(document);
  214. writer.close();
  215. /** 执行成功,需返回1 */
  216. returnValue = 1;
  217. }
  218. catch (Exception ex) {
  219. ex.printStackTrace();
  220. }
  221. return returnValue;
  222. }
  223. /**
  224. * 格式化XML文档,并解决中文问题
  225. * @param filename
  226. * @return
  227. */
  228. public int formatXMLFile(String filename) {
  229. int returnValue = 0;
  230. try {
  231. SAXReader saxReader = new SAXReader();
  232. Document document = saxReader.read(new File(filename));
  233. XMLWriter output = null;
  234. /** 格式化输出,类型IE浏览一样 */
  235. OutputFormat format = OutputFormat.createPrettyPrint();
  236. /** 指定XML字符集编码 */
  237. format.setEncoding("UTF-8");
  238. output = new XMLWriter(new FileWriter(new File(filename)), format);
  239. output.write(document);
  240. output.close();
  241. /** 执行成功,需返回1 */
  242. returnValue = 1;
  243. }
  244. catch (Exception ex) {
  245. ex.printStackTrace();
  246. }
  247. return returnValue;
  248. }
  249. }

复制代码

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文