我有以下XML并希望将其转换为Java对象。
<?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>
当我使用JAXB转换它时,我不确定相应的java类是什么。
根据我的理解,这是我迄今为止所尝试过的:
@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 :(得分:1)
这是带有JAXB注释的Java类。 这不是100%准确,但它可能对您有帮助。
<强> 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;
}
}