FileDocCategorySizeDatePackage
PlanetSearch.javaAPI DocExample3525Wed May 18 09:39:04 BST 2005com.discursive.jccook.xml.jxpath

PlanetSearch

public class PlanetSearch extends Object

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

      LogInit.init(); 
        new PlanetSearch().planetSearch();
    
public voidplanetSearch()

        List planets = new ArrayList();

        InputStream input = getClass().getResourceAsStream("./planets.xml");
        URL rules = getClass().getResource("./planet-digester-rules.xml");
        Digester digester = DigesterLoader.createDigester(rules);
        digester.push(planets);
        digester.parse( input );

        System.out.println("Number of planets: " + planets.size());
        
        System.out.println( "Planet Name where radius > 5000");
        JXPathContext context = JXPathContext.newContext( planets );
        Iterator iterator = context.iterate(".[@radius > 5000]/name");
        while( iterator.hasNext() ) {
            Object o  = (Object) iterator.next();
            System.out.println( "Object: " + o);
        }

        System.out.println( "Planet Name where a moon is named Deimos");
        iterator = context.iterate("./moons[. = 'Deimos']/../name");
        while( iterator.hasNext() ) {
            String name  = (String) iterator.next();
            System.out.println( "Planet Namet: " + name);
        }
        
        System.out.println( "Planet where Helium percentage greater than 2");
        iterator = context.iterate("./atmosphere/components/He[.>2]/../../..");
        while( iterator.hasNext() ) {
            Planet p  = (Planet) iterator.next();
            System.out.println( "Planet: " + p.getName());
        }

        System.out.println( "All of the Moon Names");
        iterator = context.iterate("./moons");
        while( iterator.hasNext() ) {
            String moon  = (String) iterator.next();
            context.getVariables().declareVariable("moonName", moon);
            String planet = (String) context.getValue("./moons[. = $moonName]/../name");
            System.out.println( "Moon: " + moon + ", \t\t\tPlanet: " + planet);
        }