Item.java.

Graphic Java
Create XML files using JSP
David Geary
Listing 4. Item.java.


package com.companyName.beans.model;

import java.io.*;

public class Item implements java.io.Serializable {
	private String name, description;
	private float price;

	public Item() { }

	public Item(String name, String description, float price) {
		setDescription(description);
		setPrice(price);
		setName(name);
	}
	// setters and getters for name, description, and
	// price omitted

	public void printXml(PrintStream s, int indent) {
		doIndent(s,indent); 	s.println("<item>");

		doIndent(s,indent, 2); 	s.println("<name>");
		doIndent(s,indent, 3); 	s.println(getName());
		doIndent(s,indent, 2); 	s.println("</name>");

		doIndent(s,indent, 2); 	s.println("<description>");
		doIndent(s,indent, 3); 	s.println(getDescription());
		doIndent(s,indent, 2); 	s.println("</description>");

		doIndent(s,indent, 2); 	s.println("<price>");
		doIndent(s,indent, 3); 	s.println(getPrice());
		doIndent(s,indent, 2); 	s.println("</price>");

		doIndent(s,indent); 	s.println("</item>");
	}
	private void doIndent(PrintStream s, int indent, int cnt) {
		for(int i=0; i < cnt; ++i) {
			doIndent(s,indent);
		}
	}
	private void doIndent(PrintStream s, int indent) {
		for(int i=0; i < indent; ++i) {
			s.print("   ");
		}
	}
}

About the Author

David Geary was the lead engineer for the Java Management API GUI toolkit, and is now an independent Java consultant. David is the author of Graphic Java—Mastering the JFC. He can be contacted at [email protected].