2016-12-05 83 views
2

我想使用JAXB將表示XML元素的String追加到現有的XML文件中。如何使用JAXB將XML字符串附加到現有的XML文件?

例如,我有以下XML在一個單一的String變量:

<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實現這一點?

+0

請閱讀[問]並相應地改進您的問題。事實上,它幾乎不會得到答案。 – Fildor

+0

格式稍作改進,答案顯而易見。 – zx485

回答

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(); 
     } 
    } 
} 
相關問題