用JDOM完成Java更新XML文件

开发 后端 前端
利用JDOM来完成Java代码对XML文件的更新。本文51CTO转载自外文博客“Tech Brainwave”的一篇技术文章。详细介绍如何利用JDOM来完成Java代码对XML文件的更新。

【51CTO快译】51CTO曾经为大家介绍过“用JDOM整合Java和XML"、“详解Java解析XML的四种方法”和“JSP实现JDOM处理数据库到XML转换的应用”。本文详细介绍Java的文档对象模型——JDOM(Java Document Object Model)提供了一个完整的用于访问基于Java的解决方案,JDOM是用Java代码控制、输出XML数据来完成这项工作的。在JDOM上明确规定了使用一个Java代码如何修改XML文档。我们首先需要下载JDOM的压缩文件并添加到项目库文件夹中,下面是对XML文件进行修改:

sample.xml

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

本文提供了一个JDOM用简单的Java程序来修改XML文件的方法。51CT0希望这篇文章能对大家有所帮助。

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

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

【51CTO译稿,非经授权谢绝转载,合作媒体转载请注明原文出处及作者!】

【编辑推荐】

  1. 用JDom整合Java和XML
  2. JSP实现JDOM处理数据库到XML转换的应用
  3. 详解Java解析XML的四种方法

 

 

 

责任编辑:佚名 来源: 51CTO.com编译
相关推荐

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编程语言持久化

2021-03-11 08:24:48

Javapoi数据脱敏

2009-06-11 17:39:55

xmljava

2009-03-24 13:27:15

NehalemIntel服务器

2009-04-01 13:36:02

Nehalem服务器苹果

2011-07-06 14:48:17

FOR XML PATXML

2010-04-16 10:42:10

Oracle存储过程

2009-12-15 14:42:54

Internet协议

2009-01-03 14:54:40

ibmdwXML

2011-03-03 09:35:04

js

2009-03-31 16:41:38

网络性能网络监控开源

2024-02-26 12:48:28

ChatGPT人工智能论文

2009-04-23 13:19:21

创建XMLXML文件Javascript

2009-09-29 15:58:22

Hibernate映射

2009-09-09 18:00:55

C# XML编程

2009-12-02 14:14:06

PHP DOM-XML
点赞
收藏

51CTO技术栈公众号