Reputation: 1
I am trying to convert xml to java object using following jaxb code. When i unmarshall the xml file i get value only for the version attribute in Settings element. Connections value is coming as null. Please help me find out my mistake(s).WHat more details can i add? PS:Kindly excuse my beginner level knowledge in java.
XML File:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xml>
<Settings version="3" xmlns="urn:Adapter-v3">
<Connections>
<Connection name ="Dev" description="DEV">
<Share version="CS" siteURL="https://example.com" />
</Connection>
<Connection name ="Dev1" description="DEV1">
<Share version="OM" siteURL="https://example.com" />
</Connection>
</Connections>
</Settings>
POJO classes:
@XmlRootElement(name="Settings", namespace="urn:Adapter-v3")
public class Settings {
Connections connections;
private String version;
@XmlElement(name="Connections")
public Connections getConnections() {
return connections;
}
public void setConnections(Connections connections) {
this.connections = connections;
}
@XmlAttribute(name="version")
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
}
@XmlRootElement(name="Connections ")
public class Connections {
List<Connection> connection;
@XmlElement(name="Connection")
public List<Connection> getConnection() {
return connection;
}
public void setConnection(List<Connection> connection) {
this.connection = connection;
}
}
@XmlRootElement(name="Connection")
public class Connection {
private String name;
private String description;
Share share;
@XmlAttribute(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlAttribute(name="description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@XmlElement(name="Share")
public Share getShare() {
return share;
}
public void setShare(Share share) {
this.share = share;
}
}
@XmlRootElement(name="Share")
public class Share {
private String version;
private String siteUrl;
@XmlAttribute(name="version")
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
@XmlAttribute(name="siteURL")
public String getSiteUrl() {
return siteUrl;
}
public void setSiteUrl(String siteUrl) {
this.siteUrl = siteUrl;
}
}
MAIN FILE
public class JAXB {
public static void main(String[] args) {
JAXBContext jaxbContext;
Settings ha = null;
File fileName = new File("Config.xml");
try {
jaxbContext = JAXBContext.newInstance(Settings.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
ha = (Settings)jaxbUnmarshaller.unmarshal(fileName);
}
catch(JAXBException e) {
e.printStackTrace();
}}
Upvotes: 0
Views: 925
Reputation: 43709
You have specified the namespace
for the @XmlRootElement
, but not for other elements. @XmlElement(name="Connections")
maps the property as Connections
element, without namespace.
Either add namespace="urn:Adapter-v3"
to all other elements, or, better add a package-info.java
to the package:
@javax.xml.bind.annotation.XmlSchema(
namespace = "urn:Adapter-v3",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.acme.foo;
Upvotes: 1