自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用JDOM完成Java更新XML文件

開發(fā) 后端 前端
利用JDOM來(lái)完成Java代碼對(duì)XML文件的更新。本文51CTO轉(zhuǎn)載自外文博客“Tech Brainwave”的一篇技術(shù)文章。詳細(xì)介紹如何利用JDOM來(lái)完成Java代碼對(duì)XML文件的更新。

【51CTO快譯】51CTO曾經(jīng)為大家介紹過(guò)“用JDOM整合Java和XML"、“詳解Java解析XML的四種方法”和“JSP實(shí)現(xiàn)JDOM處理數(shù)據(jù)庫(kù)到XML轉(zhuǎn)換的應(yīng)用”。本文詳細(xì)介紹Java的文檔對(duì)象模型——JDOM(Java Document Object Model)提供了一個(gè)完整的用于訪問(wèn)基于Java的解決方案,JDOM是用Java代碼控制、輸出XML數(shù)據(jù)來(lái)完成這項(xiàng)工作的。在JDOM上明確規(guī)定了使用一個(gè)Java代碼如何修改XML文檔。我們首先需要下載JDOM的壓縮文件并添加到項(xiàng)目庫(kù)文件夾中,下面是對(duì)XML文件進(jìn)行修改:

sample.xml

  1. <root> 
  2. <firsttag tag="file"> 
  3. <firstsubtag>first subtag</firstsubtag> 
  4. </firsttag> 
  5. <secondtag>second tag</secondtag> 
  6. </root> 
  7.  

下面的Java代碼用于更新或修改一個(gè)XML文件。

  1. import java.io.File;  
  2. import java.io.FileWriter;  
  3. import org.jdom.Document;  
  4. import org.jdom.Element;  
  5. import org.jdom.input.SAXBuilder;  
  6. import org.jdom.output.XMLOutputter;  
  7. /**  
  8. * @author giftsam  
  9. */  
  10. public class XMLModifier  
  11. {  
  12. /**  
  13. * This method is used to modify the data's of an XML file  
  14. */  
  15. private void modifyXML()  
  16. {  
  17. try  
  18. {  
  19. /**  
  20. * Initializing the SAXBuilder class  
  21. */  
  22. SAXBuilder builder = new SAXBuilder();  
  23. String filePath = "E:" + File.separator + "xml" + File.separator +"sample.xml";  
  24. System.out.println("File path is: " + filePath);  
  25. File file = new File(filePath);  
  26. if (file.exists())  
  27. {  
  28. Document document = (Document) builder.build(file);  
  29. /**  
  30. * Get the root element from the document class instance and from the root element get all the child elements and  
  31. * replace the appropriate values  
  32. */  
  33. Element root = document.getRootElement();  
  34. Element firstElement = root.getChild("firsttag");  
  35. f irstElement.getAttribute("tag").setValue("file");  
  36.  
  37. firstElement.getChild("firstsubelement").setText("test");  
  38. Element secondElement = root.getChild("secondtag");  
  39. secondElement.setText("This is the second tag");  
  40.  
  41. /**  
  42. * Print the modified xml document  
  43. */  
  44. String  xmlFileDatanew XMLOutputter().outputString(document);  
  45. System.out.println("Modified XML file is : " + xmlFileData);  
  46.  
  47. /**  
  48. * Modify the orginal document using FileWritter  
  49. */  
  50. FileWriter fileWriter = new FileWriter(file);  
  51. fileWriter.write(des);  
  52. fileWriter.close();  
  53. }  
  54. else  
  55. {  
  56. System.out.println("File does not exist");  
  57. }  
  58. }  
  59. catch (Exception ex)  
  60. {  
  61. ex.printStackTrace();  
  62. }  
  63. }  
  64.  
  65. public static void main(String argS[])  
  66. {  
  67. try  
  68. {  
  69. new XMLModifier().modifyXML();  
  70. }  
  71. catch (Exception ex)  
  72. {  
  73. ex.printStackTrace();  
  74. }  
  75. }  

下面的是修改后的XML文件。

sample.xml(Modified)

  1. <root> 
  2. <firsttag tag="test"> 
  3. <firstsubtag>This is the first sub tag</firstsubtag> 
  4. </firsttag> 
  5. <secondtag>This is the second tag</secondtag> 
  6. </root> 

本文提供了一個(gè)JDOM用簡(jiǎn)單的Java程序來(lái)修改XML文件的方法。51CT0希望這篇文章能對(duì)大家有所幫助。

原文地址:techbrainwave.com/?p=391

原文名:Java code to update an XML file using JDOM

【51CTO譯稿,非經(jīng)授權(quán)謝絕轉(zhuǎn)載,合作媒體轉(zhuǎn)載請(qǐng)注明原文出處及作者!】

【編輯推薦】

  1. 用JDom整合Java和XML
  2. JSP實(shí)現(xiàn)JDOM處理數(shù)據(jù)庫(kù)到XML轉(zhuǎn)換的應(yīng)用
  3. 詳解Java解析XML的四種方法

 

 

 

責(zé)任編輯:佚名 來(lái)源: 51CTO.com編譯
相關(guān)推薦

2011-11-17 13:04:58

JDOMJavaXML

2012-05-23 13:17:43

JavaJdomXML

2009-06-29 18:04:32

JDOM文檔JSP

2011-12-28 10:57:37

2022-03-22 09:41:31

Java編程語(yǔ)言持久化

2021-03-11 08:24:48

Javapoi數(shù)據(jù)脫敏

2009-03-24 13:27:15

NehalemIntel服務(wù)器

2009-04-01 13:36:02

Nehalem服務(wù)器蘋果

2009-06-11 17:39:55

xmljava

2011-07-06 14:48:17

FOR XML PATXML

2010-04-16 10:42:10

Oracle存儲(chǔ)過(guò)程

2009-12-15 14:42:54

Internet協(xié)議

2009-01-03 14:54:40

ibmdwXML

2024-02-26 12:48:28

ChatGPT人工智能論文

2009-03-31 16:41:38

網(wǎng)絡(luò)性能網(wǎng)絡(luò)監(jiān)控開源

2011-03-03 09:35:04

js

2009-04-23 13:19:21

創(chuàng)建XMLXML文件Javascript

2009-09-29 15:58:22

Hibernate映射

2013-06-08 17:35:46

Android開發(fā)移動(dòng)開發(fā)XML自定義菜單

2009-09-09 18:00:55

C# XML編程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)