日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
用JDOM完成Java更新XML文件

曾經(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)行修改:

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

sample.xml

 
 
 
  1.  
  2.  
  3. first subtag 
  4.  
  5. second tag 
  6.  
  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  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.  

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

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

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

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


文章題目:用JDOM完成Java更新XML文件
文章鏈接:http://www.dlmjj.cn/article/djscigj.html