import java.util.Vector;

/**
 * Examples of using XEngine
 */ 

class Test 
{
    public static void main(String[] args) {

	String xmlfile = "sample.xml";

	// parse a file using xengine 

        XEngine xEngine = new XEngine();

        if( ! xEngine.parseFile( xmlfile ) ) {
           System.out.println( "FORMATTING ERROR IN " + xmlfile  );
           return;
	}

	// Note: the XEngine elements are all Vectors 
	Vector element = null;
	// Note: and all tags attributes and values are Strings 
	String result = null;

	// find an element 
	element  = xEngine.getElement( "publisher" );

	// find attribute of the element
	result = xEngine.getElementAttr( element, "name" );
	System.out.println( result );

	// find value of a child element 
	result = xEngine.getElementValue( element , "url" );
	System.out.println( result );

	// find an element of given attribute
	element  = xEngine.getElementType( "book", "id", "bk109" );
	
	// find a child element value of given element 
	result = xEngine.getElementValue( element , "author" );
	System.out.println( result );

	// find the count of a particular element 
        int count = xEngine.getElementCount( "book" );

	// find a particlar tag value from all elements
	for( int i=0; i < count; i++){
		element = xEngine.getElement( "book", i );
		// Note: XEngine returns null if an element was not found
		if( element != null ){
			result = xEngine.getElementValue( element , "title" );
			// XEngine also returns null for values not found
			if( result != null )
				System.out.println( (i+1) + ". " + result );
		}
	}	

	// getElements can also be used to get the same results 
	Vector elements =  xEngine.getElements( "book" );
	for( int i=0; i < elements.size(); i++){
		result = xEngine.getElementValue((Vector)elements.elementAt(i), 
						"title" );
		System.out.println( (i+1) + ". " + result );
	}
	
	// you can also make nested calls without using 
        // an intermediate Vector to store an element result

	result = xEngine.getElementValue( 
			xEngine.getElement( 
				xEngine.getElement("publisher"), "address" ), 
					"city"  );
	System.out.println( result );

	// use the utility function to print the values of an element 
       	xEngine.printElement( xEngine.getElement( "publisher" ) ); 

	// more complex example gets the value of a one child element
        // if the element has a particcular value for another child element 
        
        // for example getting the description for the book "Midnight Rain"

	for( int i=0; i < count; i++){
		element = xEngine.getElement( "book", i );
		result = xEngine.getElementValue( element , "title" );
		if( result.equals("Midnight Rain") ){
			String description = xEngine.getElementValue( element, 
								"description");
			System.out.println( description );
		}
	}	

	// another example - find all books with price < 5 

	for( int i=0; i < count; i++){
		element = xEngine.getElement( "book", i );
		result = xEngine.getElementValue( element , "price" );
		float price = Float.parseFloat( result );
		if( price < 5 ){
			String title = xEngine.getElementValue( element, 
								"title");
			System.out.println( title + " " + result + "$" );
		}
	}	

    }

}



syntax highlighted by Code2HTML, v. 0.9