JAXB : creating java object from xml element atribute



I want to map a XML (which is basically exported from some database) file to java objects using JAXB.


My target XML is like-



<database name="sales">
<table name="product">
<column name="id">1</column>
<column name="name">Product 1</column>
<column name="qty">10</column>
</table>
<table name="product">
<column name="id">2</column>
<column name="name">Product 2</column>
<column name="qty">20</column>
</table>
</database>


And I am expecting a Model like below:



public class Product {
int id;
String name;
int qty;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getQty() {
return qty;
}

public void setQty(int qty) {
this.qty = qty;
}
}


And



public class Sales {
List<Product> products;

public List<Product> getProducts() {
return products;
}

public void setProducts(List<Product> products) {
this.products = products;
}
}


Some Unmarshelling example that match my requirement will be very helpful. Can anyone please help me?


No comments:

Post a Comment