如何在Java中将节点追加到现有XML文件

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文件,追加新服务器,然后将文件重写为server.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>

对代码的主要更改是 创建新的“ root”元素。上面的示例仅使用现有节点中的当前根节点server.xml,然后追加一个新的 Server

元素并重新写入该文件。

以上是 如何在Java中将节点追加到现有XML文件 的全部内容, 来源链接: utcz.com/qa/408876.html

回到顶部