av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

用JDOM完成Java更新XML文件

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

成都創(chuàng)新互聯(lián)專注于企業(yè)成都營銷網(wǎng)站建設(shè)、網(wǎng)站重做改版、銅官網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5開發(fā)、商城建設(shè)、集團公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為銅官等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

sample.xml

 
 
 
  1.  
  2.  
  3. first subtag 
  4.  
  5. second tag 
  6.  
  7.  

下面的Java代碼用于更新或修改一個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  xmlFileData= new 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.  
  2.  
  3. This is the first sub tag 
  4.  
  5. This is the second tag 
  6.  

本文提供了一個JDOM用簡單的Java程序來修改XML文件的方法。51CT0希望這篇文章能對大家有所幫助。

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

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

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


分享文章:用JDOM完成Java更新XML文件
文章鏈接:http://uogjgqi.cn/article/djscigj.html
掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流