java xml编写

yx2lnoni  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(315)

我从网上搜集了一些代码。我只需要一些附加到xml文件末尾的东西。
当我使用这段代码时,它正确地格式化了新条目,但是之前存在的xml也被进一步隔开了。我想把它加到结尾,不要碰以前的任何东西。没有 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","2") ,它只是将所有新内容输出到一行。

  1. package xml;
  2. import java.util.*;
  3. import javax.xml.transform.*;
  4. import javax.xml.transform.stream.*;
  5. import javax.xml.transform.dom.*;
  6. import org.w3c.dom.*;
  7. import javax.xml.parsers.*;
  8. public class Writer {
  9. public static void main(String[] args) throws Exception {
  10. DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
  11. DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
  12. Document document = documentBuilder.parse("input.xml");
  13. Element root = document.getDocumentElement();
  14. Element newUser = document.createElement("user");
  15. Element name = document.createElement("name");
  16. name.appendChild(document.createTextNode("test_name"));
  17. newUser.appendChild(name);
  18. Element username = document.createElement("username");
  19. username.appendChild(document.createTextNode("test_username"));
  20. newUser.appendChild(username);
  21. Element email = document.createElement("email");
  22. email.appendChild(document.createTextNode("test_email"));
  23. newUser.appendChild(email);
  24. Element password = document.createElement("password");
  25. password.appendChild(document.createTextNode("test_password"));
  26. newUser.appendChild(password);
  27. root.appendChild(newUser);
  28. DOMSource source = new DOMSource(document);
  29. TransformerFactory transformerFactory = TransformerFactory.newInstance();
  30. Transformer transformer = transformerFactory.newTransformer();
  31. transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  32. transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","2");
  33. StreamResult result = new StreamResult("input.xml");
  34. transformer.transform(source, result);
  35. }
  36. }

代码运行前的xml

  1. <?xml version = "1.0"?>
  2. <Test>
  3. <user>
  4. <name>alpha</name>
  5. <username>alpha1</username>
  6. <email>alpha@gmail.com</email>
  7. <password>a</password>
  8. </user>
  9. <user>
  10. <name>beta</name>
  11. <username>beta1</username>
  12. <email>beta@gmail.com</email>
  13. <password>b</password>
  14. </user>
  15. <user>
  16. <name>omega</name>
  17. <username>omega1</username>
  18. <email>omega@gmail.com</email>
  19. <password>w</password>
  20. </user>
  21. </Test>

代码运行后的xml

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <Test>
  3. <user>
  4. <name>alpha</name>
  5. <username>alpha1</username>
  6. <email>alpha@gmail.com</email>
  7. <password>a</password>
  8. </user>
  9. <user>
  10. <name>beta</name>
  11. <username>beta1</username>
  12. <email>beta@gmail.com</email>
  13. <password>b</password>
  14. </user>
  15. <user>
  16. <name>omega</name>
  17. <username>omega1</username>
  18. <email>omega@gmail.com</email>
  19. <password>w</password>
  20. </user>
  21. <user>
  22. <name>test_name</name>
  23. <username>test_username</username>
  24. <email>test_email</email>
  25. <password>test_password</password>
  26. </user>
  27. </Test>
56lgkhnf

56lgkhnf1#

我刚刚注意到,当我用Java11运行您的代码时,得到的结果与您相同,但是如果我改为Java8,则会得到以下结果

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <Test>
  3. <user>
  4. <name>alpha</name>
  5. <username>alpha1</username>
  6. <email>alpha@gmail.com</email>
  7. <password>a</password>
  8. </user>
  9. <user>
  10. <name>beta</name>
  11. <username>beta1</username>
  12. <email>beta@gmail.com</email>
  13. <password>b</password>
  14. </user>
  15. <user>
  16. <name>omega</name>
  17. <username>omega1</username>
  18. <email>omega@gmail.com</email>
  19. <password>w</password>
  20. </user>
  21. <user>
  22. <name>test_name</name>
  23. <username>test_username</username>
  24. <email>test_email</email>
  25. <password>test_password</password>
  26. </user>
  27. </Test>

(是的,缩进是这样的)
所以这看起来像是java/jdk中的一个bug。。我知道。。。不是一个真正的答案,但绝对有趣。

展开查看全部

相关问题