如何在Java中创建XML文件 - (DOM解析器)

x33g5p2x  于2022-10-06 转载在 Java  
字(4.5k)|赞(0)|评价(0)|浏览(915)

在这篇文章中,我们将学习如何使用DOM XML Parser在Java中创建一个XML文件。DOM提供了许多方便的类来轻松创建一个XML文件。首先,你必须用DocumentBuilder类创建一个Document,用Element类定义所有的XML内容-节点,属性。最后,使用Transformer类将整个XML内容输出到流输出,通常是一个文件。

开发过程步骤

  1. users.xml输出文件
  2. Java DOM解析器在Java中创建一个XML文件

1. users.xml输出文件

在这个例子的最后,将创建以下名为 "users.xml "的XML文件。

  1. <?xmlversion="1.0"encoding="UTF-8"standalone="no"?>
  2. <Users>
  3. <User id="1">
  4. <firstName>Ramesh</firstName>
  5. <lastName>Fadatare</lastName>
  6. <age>28</age>
  7. <gender>Male</gender>
  8. </User>
  9. <User id="2">
  10. <firstName>John</firstName>
  11. <lastName>Cena</lastName>
  12. <age>45</age>
  13. <gender>Male</gender>
  14. </User>
  15. <User id="3">
  16. <firstName>Tom</firstName>
  17. <lastName>Cruise</lastName>
  18. <age>40</age>
  19. <gender>Male</gender>
  20. </User>
  21. </Users>

2. 用Java DOM解析器来创建一个XML文件

让我们使用DOM解析器创建一个程序来在Java中创建一个XML文件。

  1. package net.javaguides.javaxmlparser.dom;
  2. import java.io.File;
  3. import javax.xml.parsers.DocumentBuilder;
  4. import javax.xml.parsers.DocumentBuilderFactory;
  5. import javax.xml.transform.OutputKeys;
  6. import javax.xml.transform.Transformer;
  7. import javax.xml.transform.TransformerFactory;
  8. import javax.xml.transform.dom.DOMSource;
  9. import javax.xml.transform.stream.StreamResult;
  10. import org.w3c.dom.Document;
  11. import org.w3c.dom.Element;
  12. import org.w3c.dom.Node;
  13. public class CreateXMLFileInJava {
  14. public static void main(String[] args) {
  15. DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  16. DocumentBuilder dBuilder;
  17. try {
  18. dBuilder = dbFactory.newDocumentBuilder();
  19. Document doc = dBuilder.newDocument();
  20. // add elements to Document
  21. Element rootElement = doc.createElement("Users");
  22. // append root element to document
  23. doc.appendChild(rootElement);
  24. // append first child element to root element
  25. rootElement.appendChild(createUserElement(doc, "1", "Ramesh", "Fadatare", "28", "Male"));
  26. // append second child
  27. rootElement.appendChild(createUserElement(doc, "2", "John", "Cena", "45", "Male"));
  28. // append third child
  29. rootElement.appendChild(createUserElement(doc, "3", "Tom", "Cruise", "40", "Male"));
  30. // for output to file, console
  31. TransformerFactory transformerFactory = TransformerFactory.newInstance();
  32. Transformer transformer = transformerFactory.newTransformer();
  33. // for pretty print
  34. transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  35. DOMSource source = new DOMSource(doc);
  36. // write to console or file
  37. StreamResult console = new StreamResult(System.out);
  38. StreamResult file = new StreamResult(new File("create_users.xml"));
  39. // write data
  40. transformer.transform(source, console);
  41. transformer.transform(source, file);
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. private static Node createUserElement(Document doc, String id, String firstName, String lastName, String age,
  47. String gender) {
  48. Element user = doc.createElement("User");
  49. // set id attribute
  50. user.setAttribute("id", id);
  51. // create firstName element
  52. user.appendChild(createUserElements(doc, user, "firstName", firstName));
  53. // create lastName element
  54. user.appendChild(createUserElements(doc, user, "lastName", lastName));
  55. // create age element
  56. user.appendChild(createUserElements(doc, user, "age", age));
  57. // create gender element
  58. user.appendChild(createUserElements(doc, user, "gender", gender));
  59. return user;
  60. }
  61. // utility method to create text node
  62. private static Node createUserElements(Document doc, Element element, String name, String value) {
  63. Element node = doc.createElement(name);
  64. node.appendChild(doc.createTextNode(value));
  65. return node;
  66. }
  67. }

上述程序的一个输出:

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <Users>
  3. <User id="1">
  4. <firstName>Ramesh</firstName>
  5. <lastName>Fadatare</lastName>
  6. <age>28</age>
  7. <gender>Male</gender>
  8. </User>
  9. <User id="2">
  10. <firstName>John</firstName>
  11. <lastName>Cena</lastName>
  12. <age>45</age>
  13. <gender>Male</gender>
  14. </User>
  15. <User id="3">
  16. <firstName>Tom</firstName>
  17. <lastName>Cruise</lastName>
  18. <age>40</age>
  19. <gender>Male</gender>
  20. </User>
  21. </Users>

注意,为了调试,你可以改变StreamResult来输出XML内容到你的控制台。
写到控制台或文件:

  1. StreamResult console = new StreamResult(System.out);
  2. StreamResult file = new StreamResult(new File("create_users.xml"));
  3. transformer.transform(source, console); // to console
  4. transformer.transform(source, file); // to file

相关文章

最新文章

更多