Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JAXB and marshalling

 
Kumar Gaurav
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Using JAXB i have to create XML file for a Java object, which i am able to create, below is the code :

@XmlRootElement (name="employee")
@XmlAccessorType (XmlAccessType.FIELD)
public class Employee implements Serializable {

@XmlElement
private String name;
@XmlElement
private String age;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}

public Employee () {

}
public Employee (String name, String age) {
this.name = name;
this.age = age;
}

public void addPlaceDetail (PlaceDetail detail) {

if (null == placeDetail){

placeDetail = new ArrayList<PlaceDetail>();
}
placeDetail.add(detail);
}

public static void main (String[] args) throws JAXBException, IOException {


Employee employee = new Employee("Nitish","25")

JAXBContext context = JAXBContext.newInstance(Employee.class);
FileWriter writer = new FileWriter("D://EmpXML.xml");
context.createMarshaller().marshal(employee, writer);
writer.close();
}

}


Problem is i am using annotations to mark root element and other element and i am not supposed to use annotations.
I have to do it using schema (may be xsd file). Can any one guide me how to do this.
 
Naren Mane
Greenhorn
Posts: 16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kumar Gaurav wrote:Hi All,

Using JAXB i have to create XML file for a Java object, which i am able to create, below is the code :

@XmlRootElement (name="employee")
@XmlAccessorType (XmlAccessType.FIELD)
public class Employee implements Serializable {

@XmlElement
private String name;
@XmlElement
private String age;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}

public Employee () {

}
public Employee (String name, String age) {
this.name = name;
this.age = age;
}

public void addPlaceDetail (PlaceDetail detail) {

if (null == placeDetail){

placeDetail = new ArrayList<PlaceDetail>();
}
placeDetail.add(detail);
}

public static void main (String[] args) throws JAXBException, IOException {


Employee employee = new Employee("Nitish","25")

JAXBContext context = JAXBContext.newInstance(Employee.class);
FileWriter writer = new FileWriter("D://EmpXML.xml");
context.createMarshaller().marshal(employee, writer);
writer.close();
}

}


Problem is i am using annotations to mark root element and other element and i am not supposed to use annotations.
I have to do it using schema (may be xsd file). Can any one guide me how to do this.


Hi Gaurav,

(I have to do it using schema (may be xsd file)) => You mean that, you want create java classes from xsd .
Correct me if i'm wrong
 
Kumar Raja
Ranch Hand
Posts: 547
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


public void addPlaceDetail (PlaceDetail detail) {

if (null == placeDetail){

placeDetail = new ArrayList<PlaceDetail>();
}
placeDetail.add(detail);
}


Is the above code correct, or am I missing anything here.
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, where is placeDetail defined?
 
Naren Mane
Greenhorn
Posts: 16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to do like java to xsd then use JiBX. Easiest way to do so, in my opinion. You don't need to write any "Xml" annotations in your java class.
 
dileep keely
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't want the annotation,then you can go for castor framework.Where in you develop the XSD and by using the "org.exolab.castor.builder.SourceGenerator",you get the generated .java files which consists of set,get along with the marshal and unmarshal methods for marshalling and unmarshalling.
I posted that at dileepkeely.blogspot.com
Just my thoughts!!






 
Crusading Chameleon likes the size of this ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!