java xml编写

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

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

package xml;

import java.util.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;

public class Writer {

    public static void main(String[] args) throws Exception {

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse("input.xml");
        Element root = document.getDocumentElement();

        Element newUser = document.createElement("user");

        Element name = document.createElement("name");
        name.appendChild(document.createTextNode("test_name"));
        newUser.appendChild(name);

        Element username = document.createElement("username");
        username.appendChild(document.createTextNode("test_username"));
        newUser.appendChild(username);

        Element email = document.createElement("email");
        email.appendChild(document.createTextNode("test_email"));
        newUser.appendChild(email);

        Element password = document.createElement("password");
        password.appendChild(document.createTextNode("test_password"));
        newUser.appendChild(password);

        root.appendChild(newUser);

        DOMSource source = new DOMSource(document);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","2");
        StreamResult result = new StreamResult("input.xml");
        transformer.transform(source, result);

    }
}

代码运行前的xml

<?xml version = "1.0"?>
<Test>

   <user>
      <name>alpha</name>
      <username>alpha1</username>
      <email>alpha@gmail.com</email>
      <password>a</password>
   </user>

   <user>
      <name>beta</name>
      <username>beta1</username>
      <email>beta@gmail.com</email>
      <password>b</password>
   </user>

   <user>
      <name>omega</name>
      <username>omega1</username>
      <email>omega@gmail.com</email>
      <password>w</password>
   </user>

</Test>

代码运行后的xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Test>

  <user>

    <name>alpha</name>

    <username>alpha1</username>

    <email>alpha@gmail.com</email>

    <password>a</password>

  </user>

  <user>

    <name>beta</name>

    <username>beta1</username>

    <email>beta@gmail.com</email>

    <password>b</password>

  </user>

  <user>

    <name>omega</name>

    <username>omega1</username>

    <email>omega@gmail.com</email>

    <password>w</password>

  </user>

  <user>
    <name>test_name</name>
    <username>test_username</username>
    <email>test_email</email>
    <password>test_password</password>
  </user>
</Test>
56lgkhnf

56lgkhnf1#

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

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Test>

   <user>
      <name>alpha</name>
      <username>alpha1</username>
      <email>alpha@gmail.com</email>
      <password>a</password>
   </user>

   <user>
      <name>beta</name>
      <username>beta1</username>
      <email>beta@gmail.com</email>
      <password>b</password>
   </user>

   <user>
      <name>omega</name>
      <username>omega1</username>
      <email>omega@gmail.com</email>
      <password>w</password>
   </user>

<user>
    <name>test_name</name>
    <username>test_username</username>
    <email>test_email</email>
    <password>test_password</password>
  </user>
</Test>

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

相关问题