gpt4 book ai didi

java - 如何将节点附加到 java 中的现有 XML 文件

转载 作者:行者123 更新时间:2023-11-28 22:01:05 25 4
gpt4 key购买 nike

public static void addALLToXML(Collection<Server> svr) throws IOException,
ParserConfigurationException, TransformerException
{
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory
.newDocumentBuilder();
Document document = documentBuilder.newDocument();

// Root Element
Element rootElement = document.createElement("Servers");
document.appendChild(rootElement);

for (Server i : svr)
{
// server elements
Element server = document.createElement("server");
rootElement.appendChild(server);

Element name = document.createElement("name");
name.appendChild(document.createTextNode(i.getName()));
server.appendChild(name);

Element port = document.createElement("port");
port.appendChild(document.createTextNode(Integer.toString(i.getPort())));
server.appendChild(port);
}

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document);

StreamResult result = new StreamResult("/home/user/server.xml");
transformer.transform(source, result);
}

这是我需要帮助的功能:

public static void addNodeToXML(String nameIn, String portIn)
throws ParserConfigurationException, SAXException, IOException
{
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory
.newDocumentBuilder();

/* parse existing file to DOM */
Document document = documentBuilder
.parse(new File("/home/user/server.xml"));

// Root Element
Element rootElement = document.createElement("Servers");
document.appendChild(rootElement);

// server elements
Element server = document.createElement("server");
rootElement.appendChild(server);

Element name = document.createElement("name");
name.appendChild(document.createTextNode(nameIn));
server.appendChild(name);

Element port = document.createElement("port");
port.appendChild(document.createTextNode(portIn));
server.appendChild(port);
}

原文:

<Servers>
<server>
<name>something</name>
<port>port</port>
</server>
</Servers>

通缉:

<Servers> 
<server>
<name>something</name>
<port>port</port>
</server>
<server>
<name>something</name>
<port>port</port>
</server>
<Servers>

最佳答案

下面的完整示例将从当前目录读取一个现有 server.xml 文件,附加一个新服务器并将该文件重新写入服务器。 xml。如果没有现有的 .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 AddXmlNode {
public static void main(String[] args) throws Exception {

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

Collection<Server> servers = new ArrayList<Server>();
servers.add(new Server());

for (Server server : servers) {
// server elements
Element newServer = document.createElement("server");

Element name = document.createElement("name");
name.appendChild(document.createTextNode(server.getName()));
newServer.appendChild(name);

Element port = document.createElement("port");
port.appendChild(document.createTextNode(Integer.toString(server.getPort())));
newServer.appendChild(port);

root.appendChild(newServer);
}

DOMSource source = new DOMSource(document);

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
StreamResult result = new StreamResult("server.xml");
transformer.transform(source, result);
}

public static class Server {
public String getName() { return "foo"; }
public Integer getPort() { return 12345; }
}
}

server.xml 文件示例:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Servers>
<server>
<name>something</name>
<port>port</port>
</server>
</Servers>

代码的主要变化是创建新的“根”元素。上面的示例仅使用现有 server.xml 中的当前根节点,然后附加一个新的 Server 元素并重新写入文件。

关于java - 如何将节点附加到 java 中的现有 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48364527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com