15-Jun-17 (Created: 15-Jun-17) | More in 'Old Jaxb 2.0 Tutorial 2014'

jaxb 2.0 tutorial: Java classes

Manage this page

1. Back to tutorial

Content.java


@XmlRootElement
@XmlAccessorType(AccessType.FIELD)
public class Content 
{
   @XmlElements({@XmlElement(name="folder", type=Folder.class),
   @XmlElement(name="file", type=FileItem.class)
   })
  	public List children = new ArrayList();
	public void add(Folder f)
	{
		children.add(f);
	}
	public void add(FileItem f)
	{
		children.add(f);
	}
}

Folder.java


package com.ai.xml.jaxb20;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.AccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;

@XmlAccessorType(AccessType.FIELD)
public class Folder 
{
	@XmlAttribute
	public String id;
	
   @XmlElements({@XmlElement(name="folder", type=Folder.class),
		   @XmlElement(name="file", type=FileItem.class)
		   })
	public List files = new ArrayList();
	public Folder(){}
	public Folder(String inId)
	{
		id = inId;
	}
	public void addFile(FileItem f)
	{
		files.add(f);
	}
}

FileItem.java


package com.ai.xml.jaxb20;

import javax.xml.bind.annotation.*;

@XmlAccessorType(AccessType.FIELD)
public class FileItem 
{
	@XmlAttribute (name="name")
	public String fileName;
	
	@XmlElement (name="description")
	public String fileDescription;
	
	@XmlElement (name="content")
	public String fileContent;
	public FileItem(String name, String description,String content)
	{
		fileName = name;
		fileDescription=description;
		fileContent = content;
	}
	public FileItem()
	{
	}
}