首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

JDOM 创造 读取 修改 删除 XML文件

2013-03-12 
JDOM 创建 读取 修改 删除 XML文件转自:http://takeme.iteye.com/blog/1825967?加入?D:\Soft\jar\jdom.jar

JDOM 创建 读取 修改 删除 XML文件

转自:http://takeme.iteye.com/blog/1825967

?

加入?
D:\Soft\jar\jdom.jar?

Java代码??JDOM 创造 读取 修改 删除 XML文件
  1. package?com.itheima.xml;??
  2. ??
  3. import?java.io.File;??
  4. import?java.io.FileWriter;??
  5. import?java.util.Iterator;??
  6. import?java.util.List;??
  7. ??
  8. import?org.jdom.Attribute;??
  9. import?org.jdom.Document;??
  10. import?org.jdom.Element;??
  11. import?org.jdom.input.SAXBuilder;??
  12. import?org.jdom.output.Format;??
  13. import?org.jdom.output.XMLOutputter;??
  14. ??
  15. public?class?TextJDOMparseXML?{//操作使用JDOM解析XML文件的类??
  16. ??
  17. ????/**?
  18. ?????*?@param?args?
  19. ?????*/??
  20. ????public?static?void?main(String[]?args)?{??
  21. ????????//?TODO?Auto-generated?method?stub??
  22. ????????String?fileName="F:/test1.xml";??
  23. ????????TextJDOMparseXML?jdomXml=new?TextJDOMparseXML();??
  24. ????????System.out.println("===============1.运用jdom创建XML文件");??
  25. ????????if(jdomXml.createXMLFile(fileName,?"GB2312")==1)??
  26. ????????????System.out.println("运用JDOM创建XML文件成功!");??
  27. ????????else??
  28. ????????????System.out.println("运用JDOM创建XML文件失败!");??
  29. ????????System.out.println("===============2.运用JDOM读取XML文件");??
  30. ????????jdomXml.read(jdomXml.getDocument(fileName));??
  31. ????????System.out.println("===============3.修改XML内容,输出修改后的文档");??
  32. ????????jdomXml.update(fileName,?"rongshu",?"liushu");??
  33. ????????System.out.println("===============4.删除编号为002的节点,输出修改后的文件");??
  34. ????????jdomXml.delete(fileName,?"002");??
  35. ??????????
  36. ????}??
  37. ??????
  38. ????//生成一个新的Document????
  39. ????public?Document?createDocument(){??
  40. ????????Document?document=new?Document();??
  41. ????????return?document;??
  42. ????}??
  43. ????//创建一个节点??
  44. ????public?Element?createElement(String?paramName,String?paramValue){??
  45. ????????Element?element=new?Element(paramName);??
  46. ????????element.setText(formatTextForXML(paramValue));??
  47. ????????return?element;??
  48. ????}??
  49. ????//创建?XML文件??
  50. ????public?int?createXMLFile(String?fileName,String?encoding){??
  51. ????????int?returnValue=0;??
  52. ????????Document?document=createDocument();??
  53. ????????Element?treesElement=createElement("trees",?null);??
  54. ????????Element?treeElement=new?Element("tree");??
  55. ????????treeElement.setAttribute("id","001");??
  56. ????????treeElement.setAttribute("name","rongshu");??
  57. ??????????
  58. ????????Element?typeElement=new?Element("type");??
  59. ????????typeElement.setText("style");??
  60. ????????typeElement.setAttribute("id","xiaoshu");??
  61. ????????treeElement.addContent(typeElement);??//添加?子节点??
  62. ??????????
  63. ??????????
  64. ????????Element?descElement=new?Element("description");??
  65. ????????descElement.setText(formatTextForXML("It?is?a?big?tree"));??
  66. ????????treeElement.addContent(descElement);??
  67. ??????????
  68. ????????treesElement.addContent(treeElement);??
  69. ??????????
  70. ????????Element?tree1Element=new?Element("tree");??
  71. ????????tree1Element.setAttribute("id","002");??
  72. ????????tree1Element.setAttribute("name","yangshu");??
  73. ????????Element?type1Element=new?Element("type");??
  74. ????????type1Element.setText("style");??
  75. ????????type1Element.setAttribute("id","dashu");??
  76. ????????tree1Element.addContent(type1Element);??
  77. ????????Element?desc1Element=new?Element("description");??
  78. ????????desc1Element.setText(formatTextForXML("The?tree?grows?taller?from?day?to?day"));??
  79. ????????tree1Element.addContent(desc1Element);??
  80. ????????treesElement.addContent(tree1Element);??
  81. ??????????
  82. ??????????
  83. ????????Element?tree2Element=new?Element("tree");??
  84. ????????tree2Element.setAttribute("id","003");??
  85. ????????tree2Element.setAttribute("name","songshu");??
  86. ????????Element?desc2Element=new?Element("description");??
  87. ????????desc2Element.setText(formatTextForXML("The?tree?is?very?beautiful"));??
  88. ????????tree2Element.addContent(desc2Element);??
  89. ????????treesElement.addContent(tree2Element);??
  90. ??????????
  91. ????????Element?flowerElement=new?Element("flower");??
  92. ????????flowerElement.setText("rose");??
  93. ????????treesElement.addContent(flowerElement);??
  94. ??????????
  95. ????????document.addContent(treesElement);??
  96. ????????try?{??
  97. ????????????saveFile(fileName,document,encoding);??
  98. ????????????returnValue=1;??
  99. ????????}?catch?(Exception?e)?{??
  100. ????????????//?TODO:?handle?exception??
  101. ????????????System.out.println("创建XML文件失败:"+e.getMessage());??
  102. ????????}??
  103. ????????return?returnValue;??
  104. ??????????
  105. ????}??
  106. ????//读取xml??
  107. ????public?void?read(Document?document){??
  108. ????????try?{??
  109. ????????????Format?format=Format.getPrettyFormat();???
  110. ????????????format.setEncoding("GB2312");??
  111. ??????????????
  112. ????????????XMLOutputter?xmlOutputter=new?XMLOutputter(format);??
  113. ????????????xmlOutputter.output(document,?System.out);??
  114. ????????}?catch?(Exception?e)?{??
  115. ????????????System.out.println("读取XML文件失败:"+e.getMessage());??
  116. ????????}??
  117. ????}??
  118. ????//修改?xml文档节点??
  119. ????public?int?update(String?fileName,String?oldName,String?newName){??
  120. ????????int?returnValue=0;??
  121. ????????Document?document=getDocument(fileName);??
  122. ????????List?list=document.getRootElement().getChildren();??
  123. ????????for?(int?i?=?0;?i?<?list.size();?i++)?{??
  124. ????????????Element?element=(Element)list.get(i);??
  125. ????????????Attribute?attribute=element.getAttribute("name");??
  126. ????????????if(attribute!=null){??
  127. ????????????????if(attribute.getValue().equals(oldName)){??
  128. ????????????????????attribute.setValue(newName);??
  129. ????????????????}??
  130. ????????????}??
  131. ????????}??
  132. ??????????
  133. ????????list=document.getRootElement().getChildren("flower");??
  134. ????????Iterator?iter=list.iterator();??
  135. ????????if(iter.hasNext()){??
  136. ????????????Element?el=(Element)iter.next();??
  137. ????????????el.setText("lily");??
  138. ????????????Element?date=new?Element("date");??
  139. ????????????date.setText("2009-04-10");??
  140. ????????????date.setAttribute("type","calendar");??
  141. ????????????el.addContent(date);??
  142. ????????}??
  143. ??????????
  144. ????????try?{??
  145. ????????????showFile(fileName,document);??
  146. ????????????returnValue=1;??
  147. ????????}?catch?(Exception?e)?{??
  148. ????????????//?TODO:?handle?exception??
  149. ????????????System.out.println("修改XML文件出错:"+e.getMessage());??
  150. ????????}??
  151. ??????????
  152. ????????return?returnValue;??
  153. ??????????
  154. ??????????
  155. ????}??
  156. ????//删除XML?节点??
  157. ????public?boolean?delete(String?fileName,String?id){??
  158. ????????boolean?returnVlalue=false;??
  159. ????????Document?doc=getDocument(fileName);??
  160. ????????Element?root=doc.getRootElement();??
  161. ????????List?list=root.getChildren("tree");??
  162. ????????for?(int?i?=?0;?i?<?list.size();?i++)?{??
  163. ????????????Element?element=(Element)list.get(i);??
  164. ????????????if?(element.getAttribute("id").getValue().equals(id))?{??
  165. ????????????????root.removeContent(element);??
  166. ????????????}??
  167. ????????}??
  168. ????????try?{??
  169. ????????????showFile(fileName,?doc);??
  170. ????????????returnVlalue=true;??
  171. ????????}?catch?(Exception?e)?{??
  172. ????????????//?TODO:?handle?exception??
  173. ????????????e.printStackTrace();??
  174. ????????}??
  175. ????????return?returnVlalue;??
  176. ??????????????????
  177. ????}??
  178. ??????
  179. ????private?void?showFile(String?fileName,?Document?document)?{??
  180. ????????//?TODO?Auto-generated?method?stub??
  181. ????????Format?format=Format.getPrettyFormat();??
  182. ????????format.setEncoding("GB2312");??
  183. ????????format.setIndent(" ");??
  184. ????????try?{??
  185. ????????????XMLOutputter?xmlOutputter=new?XMLOutputter(format);??
  186. ????????????FileWriter?fileWriter=new?FileWriter(fileName);??
  187. ????????????xmlOutputter.output(document,?fileWriter);??
  188. ????????????xmlOutputter.output(document,?System.out);??
  189. ????????}?catch?(Exception?e)?{??
  190. ????????????System.out.println("XML文件输出到控制台出错:"+e.getMessage());??
  191. ????????}??
  192. ????}??
  193. ??
  194. ????private?Document?getDocument(String?fileName)?{??
  195. ????????//?TODO?Auto-generated?method?stub??
  196. ????????Document?document=null;??
  197. ????????try?{??
  198. ????????????SAXBuilder?saxBuilder=new?SAXBuilder();??
  199. ????????????document=saxBuilder.build(new?File(fileName));??
  200. ????????}?catch?(Exception?e)?{??
  201. ????????????//?TODO:?handle?exception??
  202. ????????????System.out.println("载入失败:"+e.getMessage());??
  203. ????????????return?null;??
  204. ????????}??
  205. ????????return?document;??
  206. ????}??
  207. ??
  208. ????private?boolean?saveFile(String?fileName,?Document?document,?String?encoding)?{??
  209. ????????//?TODO?Auto-generated?method?stub??
  210. ????????Format?format=Format.getPrettyFormat();??
  211. ????????if(encoding!=null){??
  212. ????????????format.setEncoding(encoding);??
  213. ????????}??
  214. ????????format.setIndent("?");?//设置xml文件的缩进??
  215. ????????try?{??
  216. ????????????XMLOutputter?xmlOutputter=new?XMLOutputter(format);??
  217. ????????????FileWriter?fileWriter=new?FileWriter(fileName);??
  218. ????????????xmlOutputter.output(document,?fileWriter);??
  219. ????????}?catch?(Exception?e)?{??
  220. ????????????System.out.println("保存文件出错:"+e.getMessage());??
  221. ????????????return?false;??
  222. ????????}??
  223. ????????return?true;??
  224. ????}??
  225. ??
  226. ????private?String?formatTextForXML(String?sourceString)?{??
  227. ????????//?TODO?Auto-generated?method?stub??
  228. ????????if?(sourceString==null)?{??
  229. ????????????return?null;??
  230. ????????}??
  231. ????????int?stLen=0;??
  232. ????????StringBuffer?reString=?new?StringBuffer();??
  233. ????????String?deString="";??
  234. ????????stLen=sourceString.length();??
  235. ????????for?(int?i?=?0;?i?<?stLen;?i++)?{??
  236. ????????????char?ch=sourceString.charAt(i);??
  237. ????????????switch?(ch)?{??
  238. ????????????case?'<':??
  239. ????????????????deString="<";??
  240. ????????????????break;??
  241. ????????????case?'>':??
  242. ????????????????deString=">";??
  243. ????????????????break;??
  244. ????????????case?'"':??
  245. ????????????????deString=""";??
  246. ????????????????break;??
  247. ????????????case?'&':??
  248. ????????????????deString="&";??
  249. ????????????????break;??
  250. ????????????case?13:??
  251. ????????????????deString="\n";??
  252. ????????????????break;??
  253. ????????????default:??
  254. ????????????????deString=""+ch;??
  255. ????????????????break;??
  256. ????????????}??
  257. ????????????reString.append(deString);??
  258. ????????}??
  259. ????????return?reString.toString();??
  260. ????}?????
  261. ??????
  262. ??????
  263. } ?

热点排行