Java tutorial
import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlTransient; @XmlRootElement(name = "part") public class Product implements Serializable { public static Product fromXML(InputStream in) throws Exception { JAXBContext context = JAXBContext.newInstance(Product.class); Unmarshaller um = context.createUnmarshaller(); return (Product) um.unmarshal(in); } private static final long serialVersionUID = 1L; @XmlElement(name = "Product_Number") private String partNumber; @XmlElement(name = "Product_Type") private String partType; @XmlElement(name = "Product_Version") private String partVersion; @XmlElement(name = "Status") private String status; @XmlElement(name = "part") private Collection<Product> subProducts = null; public static void main(String args[]) throws JAXBException { Product mainProduct = new Product("1m", "m", "1", "draft"); Product level1 = new Product("l1", "l1", "11", "new"); level1.addProduct(new Product("l2", "l2", "1", "new")); level1.addProduct(new Product("l2-2", "l2", "1", "new")); mainProduct.addProduct(level1); System.out.println(mainProduct.toXML()); } public Product() { } public Product(String no, String type, String ver, String stat) { this.partNumber = no; this.partType = type; this.partVersion = ver; this.status = stat; } @XmlTransient public String getProductNumber() { return this.partNumber; } public void setProductNumber(String partNumber) { this.partNumber = partNumber; } @XmlTransient public String getProductType() { return this.partType; } public void setProductType(String partType) { this.partType = partType; } @XmlTransient public String getProductVersion() { return this.partVersion; } public void setProductVersion(String partVersion) { this.partVersion = partVersion; } @XmlTransient public String getStatus() { return this.status; } public void setStatus(String status) { this.status = status; } @XmlTransient public Collection<Product> getSubparts() { return this.subProducts; } public void setSubparts(Collection<Product> subparts) { this.subProducts = subparts; } public void addProduct(Product part) { if (this.subProducts == null) { this.subProducts = new ArrayList<Product>(); } this.subProducts.add(part); } public void addProducts(Collection<Product> parts) { if (this.subProducts == null) { this.subProducts = new ArrayList<Product>(); } this.subProducts.addAll(parts); } public String toXML() throws JAXBException { ByteArrayOutputStream out = new ByteArrayOutputStream(); JAXBContext context = JAXBContext.newInstance(Product.class); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.marshal(this, out); return out.toString(); } }