You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by MASTRELLA STEFANO <sm...@sogei.it> on 2008/02/19 12:41:10 UTC

commons-jxpath: bean graph traverse

Hi all,
 
I'm in trouble on traversing object graph and perform query with JXPath
over a beans.
My objects are:

*************************************************
********       OBJECT 1 definition       ********
*************************************************
import java.util.HashMap;
import java.util.Map;

public class Object1
{
  private Map<String, Object2> objectsType2 = new HashMap<String,
Object2>();

  public Map getObject2 ()
  {
    return objectsType2 ;
  }
}

*************************************************
********       OBJECT 2 definition       ********
*************************************************
import java.util.HashMap;
import java.util.Map;

public class Object2
{
  private int id;
  
  private int state;
  
  private Map<String, Object3> objectsType3 = new HashMap<String,
Object3>();
  
  public Object2(int id, int state)
  {
    this.id = id;
    
    this.state= state;
  }

  public Map getObject3 ()
  {
    return objectsType3;
  }

  public int getId ()
  {
    return id;
  }

  public int getstate ()
  {
    return state;
  }
}

*************************************************
********       OBJECT 3 definition       ********
*************************************************

public class Object3
{
  private int id;

  private int state;

  public Object3(int id, int state)
  {
    this.id = id;

    this.state= state;
  }

  /**
   * @return the id
   */
  public String getId ()
  {
    return Integer.toString(id);
  }

  public int getState()
  {
    return state;
  }
}

The graph has been loaded in a static way just for the sake of clarity

*************************************************
********          BEAN loading           ********
*************************************************


public static Object1 loadObjects()
{
  Object1 model = new Object1();

  for (int i1 = 1; i1 <= 2; i1++)
  {
    model.getObject2().put(Integer.toString(i1), new Object2(i1, i1));

    for (int i2 = 1; i2 <= 100; i2++)
    {
      ((Object2)
model.getObject2().get(Integer.toString(i1))).getObject3().put(Integer.t
oString(i2), new Object3(i2, 1));
    }
  }

  return model;
}

Now I'm ready to instantiate the context

*************************************************
********    Context instantiation        ********
*************************************************

Object1 obj1 = loadObjects();

JXPathContext context = JXPathContext.newContext(obj1);

And perform some query

*************************************************
********         Query perform           ********
*************************************************

/*
 * Search for all the Object3 associated to key 34 in the map
 */
List<Object3> objs = context.selectNodes("//object3[@name='34']");

for (Object3 object3 : objs)
{
  //We found 2 objects
  System.out.println(object3.getId());
}

and this is ok. It's also ok the following fragment

List<Integer> objs2 =
context.selectNodes("//object3[@name='34']/state");

for (Integer i : objs2)
{
  //Both the selected Object3 has state equals to 1
  System.out.println(i);
}

But what if I'd like to perform a query to retrieve all the Objects3
with the state attribute equals to 1?
Writing something like that

          //object3/[@state == '1']

won't work because the step //object3 in the xpath query obtains a Map.
How can I write my string to select those kinds of Object3?

TIA,

Stefano.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org