如何使用JAXB将XML字符串附加到现有XML文件?

时间:2016-12-05 10:19:58

标签: java xml jaxb

我想使用JAXB将表示XML元素的String追加到现有的XML文件中。

例如,我在单个String变量中使用XML:

<servername>aaaaaa</servername>
<ipaddress>121212121</ipaddress>
<port>8080</port>

我希望将其包含在我的XML中的另一个<server>标记中 我现有的XML文件包含以下内容:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<servers>
    <server>
        <servername>bbbbbb</servername>
        <ipaddress>11.11.11.11</ipaddress>
        <port>8080</port>
    </server>
</servers>

我想将String变量的内容添加到此文件中,以使其内容如下:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<servers>
    <server>
        <servername>bbbbbb</servername>
        <ipaddress>11.11.11.11</ipaddress>
         <port>8080</port>
    </server>
    <server>
         <servername>aaaaaa</servername>
         <ipaddress>121212121</ipaddress>
         <port>8080</port>
    </server>
</servers>

如何使用JAXB实现此目的?

1 个答案:

答案 0 :(得分:1)

最后我自己找到了答案

<强> Server.java

package com.tools;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "server")
public class Server {

    private String servername;
    private String ipaddress;
    private int port;

    public Server() {
    }

    public Server(String servername, String ipaddress, int port) {
        super();
        this.servername = servername;
        this.ipaddress = ipaddress;
        this.port = port;
    }

    @XmlElement
    public String getServerName() {
        return servername;
    }

    public void setServerName(String servername) {
        this.servername = servername;
    }

    @XmlElement
    public String getIpAddress() {
        return ipaddress;
    }

    public void setIpAddress(String ipaddress) {
        this.ipaddress = ipaddress;
    }

    @XmlElement
    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    @Override
    public String toString() {
        return servername + " " + ipaddress + " " + port;
    }
}

<强> Servers.java

package com.tools;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;

@XmlRootElement(name = "servers")
public class Servers {

    private List<Server> servers;

    public List<Server> getServers() {
        return servers;
    }

    @XmlElement(name = "server")
    public void setServers(List<Server> servers) {
        this.servers = servers;
    }

    @Override
    public String toString() {
        return servers.toString();
    }

}

主要文件

package com.tools;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class ToolConfiguration {
    public static void main(String args[])
    {
        String configFileName = "/config.xml";
        File configFile = new File(ToolConfiguration.class.getResource(configFileName).getFile());
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Servers.class);
            // Read XML
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            Servers servers = (Servers) unmarshaller.unmarshal(configFile);

            Server server = new Server("abv", "1.9.3.5", 8080);

            List<Server> serversList = servers.getServers();
            serversList.add(server);

            servers.setServers(serversList);

            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.marshal(servers, configFile);

            /*List<Server> serversList = servers.getServers();
            for (Server server : serversList) {
                System.out.println(server.toString());
            }*/

        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}