FileDocCategorySizeDatePackage
TestDOM.javaAPI DocExample1196Sat Apr 23 22:35:42 BST 2005None

TestDOM

public class TestDOM extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder parser = factory.newDocumentBuilder();
		Document document = parser.parse( "zooinventory.xml" );
		Element inventory = document.getDocumentElement();
		NodeList animals = inventory.getElementsByTagName("Animal");

		System.out.println("Animals = ");
		for( int i=0; i<animals.getLength(); i++ ) {
			String name = DOMUtil.getSimpleElementText( 
				(Element)animals.item(i),"Name" );
			String species = DOMUtil.getSimpleElementText( 
				(Element)animals.item(i), "Species" );
			System.out.println( "  "+ name +" ("+species+")" );
		}

		Element foodRecipe = DOMUtil.getFirstElement( 
			(Element)animals.item(1), "FoodRecipe" );
		String name = DOMUtil.getSimpleElementText( foodRecipe, "Name" );
		System.out.println("Recipe = " + name );
		NodeList ingredients = foodRecipe.getElementsByTagName("Ingredient");
		for(int i=0; i<ingredients.getLength(); i++) 
			System.out.println( "  " + DOMUtil.getSimpleElementText( 
				(Element)ingredients.item(i) ) );