• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

JAXB and marshalling

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 16
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, where is placeDetail defined?
 
Naren Mane
Greenhorn
Posts: 16
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!






 
It will give me the powers of the gods. Not bad for a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic