Java tutorial
import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.Stack; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; public class TestModelBuilder { public static void main(String[] args) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); XMLReader parser = saxParser.getXMLReader(); SAXModelBuilder mb = new SAXModelBuilder(); parser.setContentHandler(mb); parser.parse(new InputSource("zooinventory.xml")); Element2 inventory = (Element2) mb.getModel(); System.out.println("Animals = " + inventory.getAnimals()); Element3 cocoa = (Element3) (inventory.getAnimals().get(1)); ElementA recipe = cocoa.getFoodRecipe(); System.out.println("Recipe = " + recipe); } } class SimpleElement { StringBuffer text = new StringBuffer(); public void addText(String s) { text.append(s); } public String getText() { return text.toString(); } public void setAttributeValue(String name, String value) { throw new Error(getClass() + ": No attributes allowed"); } } class ElementA extends SimpleElement { String name; List<String> values = new ArrayList<String>(); public void setName(String name) { this.name = name; } public String getName() { return name; } public void addIngredient(String ingredient) { values.add(ingredient); } public void setValues(List<String> ingredients) { this.values = ingredients; } public List<String> getIngredients() { return values; } public String toString() { return name + ": " + values.toString(); } } class Element2 extends SimpleElement { List<Element3> value = new ArrayList<Element3>(); public void addAnimal(Element3 animal) { value.add(animal); } public List<Element3> getAnimals() { return value; } public void setValue(List<Element3> animals) { this.value = animals; } } class Element3 extends SimpleElement { public final static int MAMMAL = 1; int animalClass; String tag1, tag2, tag3, tag4, tag5; ElementA foodRecipe; public void setTag1(String name) { this.tag1 = name; } public String getName() { return tag1; } public void setTag2(String species) { this.tag2 = species; } public String getSpecies() { return tag2; } public void setTag3(String habitat) { this.tag3 = habitat; } public String getHabitat() { return tag3; } public void setTag4(String food) { this.tag4 = food; } public String getFood() { return tag4; } public void setFoodRecipe(ElementA recipe) { this.foodRecipe = recipe; } public ElementA getFoodRecipe() { return foodRecipe; } public void setTag5(String temperament) { this.tag5 = temperament; } public String getTemperament() { return tag5; } public void setAnimalClass(int animalClass) { this.animalClass = animalClass; } public int getAnimalClass() { return animalClass; } public void setAttributeValue(String name, String value) { if (name.equals("class") && value.equals("mammal")) setAnimalClass(MAMMAL); else throw new Error("No such attribute: " + name); } public String toString() { return tag1 + "(" + tag2 + ")"; } } class SAXModelBuilder extends DefaultHandler { Stack<SimpleElement> stack = new Stack<SimpleElement>(); SimpleElement element; public void startElement(String namespace, String localname, String qname, Attributes atts) throws SAXException { SimpleElement element = null; try { element = (SimpleElement) Class.forName(qname).newInstance(); } catch (Exception e) { } if (element == null) element = new SimpleElement(); for (int i = 0; i < atts.getLength(); i++) element.setAttributeValue(atts.getQName(i), atts.getValue(i)); stack.push(element); } public void endElement(String namespace, String localname, String qname) throws SAXException { element = stack.pop(); if (!stack.empty()) try { setProperty(qname, stack.peek(), element); } catch (Exception e) { throw new SAXException("Error: " + e); } } public void characters(char[] ch, int start, int len) { String text = new String(ch, start, len); stack.peek().addText(text); } void setProperty(String name, Object target, Object value) throws SAXException { Method method = null; try { method = target.getClass().getMethod("add" + name, value.getClass()); } catch (NoSuchMethodException e) { } if (method == null) try { method = target.getClass().getMethod("set" + name, value.getClass()); } catch (NoSuchMethodException e) { } if (method == null) try { value = ((SimpleElement) value).getText(); method = target.getClass().getMethod("add" + name, String.class); } catch (NoSuchMethodException e) { } try { if (method == null) method = target.getClass().getMethod("set" + name, String.class); method.invoke(target, value); } catch (Exception e) { throw new SAXException(e.toString()); } } public SimpleElement getModel() { return element; } } //File: zooinventory.xml /*<?xml version="1.0" encoding="UTF-8"?> <Element2> <Element3 animalClass="mammal"> <Name>A</Name> <Species>B</Species> <Habitat>C</Habitat> <Food>D</Food> <Temperament>E</Temperament> <Weight>1</Weight> </Element3> <Element3 animalClass="mammal"> <Name>F</Name> <Species>G</Species> <Habitat>H</Habitat> <ElementA> <Name>I</Name> <Ingredient>I1</Ingredient> <Ingredient>I2</Ingredient> <Ingredient>I2</Ingredient> </ElementA> <Temperament>J</Temperament> <Weight>4</Weight> </Element3> </Element2> */