2016-06-20 14 views
0

Ich habe die folgende XML und möchte es in Java-Objekt konvertieren.konvertieren xml zu Java-Objekt mit JAXB

<?xml version="1.0" encoding="UTF-8"?> 
<datasources xmlns="http://www.jboss.org/ironjacamar/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.jboss.org/ironjacamar/schema http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd"> 
<datasource jndi-name="java:jboss/datasources/FDMS_DemoDS" pool-name="FDMS_DemoDS"> 
     <connection-url>jdbc:mysql://localhost:3306/demo?zeroDateTimeBehavior=convertToNull</connection-url> 
     <driver>com.mysql</driver> 
     <pool> 
      <max-pool-size>60</max-pool-size> 
     </pool> 
     <security> 
      <user-name>fduser</user-name> 
      <password>fdms!</password> 
     </security> 
    </datasource> 
</datasources> 

Ich bin nicht sicher, was meine entsprechende Java-Klasse sein, wenn ich JAXB verwende es zu konvertieren.

Dies ist, was ich bisher versucht, basierend auf meinem Verständnis:

@XmlRootElement 
public class Datasources { 

    String connectionUrl; 
    String maxPoolSize; 
    String driver; 

    public String getConnectionUrl() { 
     return connectionUrl; 
    } 

    @XmlElement 
    public void setConnectionUrl(String connectionUrl) { 
     this.connectionUrl = connectionUrl; 
    } 

    public String getMaxPoolSize() { 
     return maxPoolSize; 
    } 

    @XmlElement 
    public void setMaxPoolSize(String maxPoolSize) { 
     this.maxPoolSize = maxPoolSize; 
    } 

    public String getDriver() { 
     return driver; 
    } 

    @XmlElement 
    public void setDriver(String driver) { 
     this.driver = driver; 
    } 
} 
+0

@dabadaba ich nicht in der Lage bin POJO für gegebene xml zu erstellen. –

+0

Ihr Server ist da [datasources_1_0.xsd] (http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd). Laden Sie die Datei herunter und generieren Sie Ihre Java-Datei über Jaxb. – Patrick

+0

Die Klasse, die Sie gebucht haben, ist mehr für das Objekt "Datasource" als das Objekt "Datasources". – Berger

Antwort

0

mit der JAXB Annotation die Java-Klassen hier. Dies ist nicht 100% genau, aber es kann Ihnen helfen.

Datasources.java

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "datasource" 
}) 
public class Datasources { 

    @XmlElement(name = "datasource") 
    private List<Datasource> datasources; 

    public List<Datasource> getDatasources() { 
     if (datasource == null) { 
      datasources = new ArrayList<Datasource>(); 
     } 
     return datasources 
    } 
} 

Datasource.java

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "connection-url", 
    "driver", 
    "pool", 
    "security" 
}) 
public class Datasource { 

    @XmlElement(name = "connection-url") 
    private String connectionUrl; 
    @XmlElement(name = "driver") 
    private String driver; 
    @XmlElement(name = "pool") 
    private Pool pool; 
    @XmlElement(name = "security") 
    private Security security; 

    public String getConnectionUrl() { 
     return connectionUrl; 
    } 

    public void setConnectionUrl(String value) { 
     this.connectionUrl = value; 
    } 

    public String getDriver() { 
     return driver; 
    } 

    public void setDriver(String value) { 
     this.driver = value; 
    } 

    public Pool getPool() { 
     return pool; 
    } 

    public void setPool(Pool value) { 
     this.pool = value; 
    } 

    public Security getSecurity() { 
     return security; 
    } 

    public void setSecurity(Security value) { 
     this.security = value; 
    } 
} 

Pool.java

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "max-pool-size" 
}) 
public class Pool { 
    @XmlElement(name = "max-pool-size") 
    private String maxPoolSize; 

    public String getMaxPoolSize() { 
     return maxPoolSize; 
    } 

    public void setMaxPoolSize(String value) { 
     this.maxPoolSize = value; 
    } 
} 

Security.java

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "user-name", 
    "password" 
}) 
public class Security { 
    @XmlElement(name = "user-name") 
    private String username; 
    @XmlElement(name = "password") 
    private String password; 

    public String getUsername() { 
     return username; 
    } 

    public void setUsername(String value) { 
     this.username = value; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String value) { 
     this.password = value; 
    } 
}