tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > JXPath > How to use Relative Context

How to use Relative Context 

JXPath is a java library for Object Graph Navigation using the XPath syntax. This requires the libraries commons-jxpath-1.3.jar, commons-beanutils.jar and commons-logging.jar to be in classpath. The following example shows creating relative context in JXPath.

File Name  :  
com/bethecoder/tutorials/jxpath/common/Student.java 
   
package com.bethecoder.tutorials.jxpath.common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Student {

  private String name;
  private int age;
  private String hobby;
  private List<String> nickNames  = new ArrayList<String>();

  public Student() {
  }

  public Student(String name, int age, String hobby) {
    super();
    this.name = name;
    this.age = age;
    this.hobby = hobby;
  }
  
  public Student(String name, int age, String hobby, String [] nickNames) {
    super();
    this.name = name;
    this.age = age;
    this.hobby = hobby;
    this.nickNames = Arrays.asList(nickNames);
  }

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public String getHobby() {
    return hobby;
  }
  public void setHobby(String hobby) {
    this.hobby = hobby;
  }

  public List<String> getNickNames() {
    return nickNames;
  }

  public void setNickNames(List<String> nickNames) {
    this.nickNames = nickNames;
  }
  
  public String toString() {
    return "Student[name = " + name + ", age = " + age + ", hobby = " + hobby + ", " + nickNames + "]";
  }

}
   

File Name  :  
com/bethecoder/tutorials/jxpath/common/StudentClass.java 
   
package com.bethecoder.tutorials.jxpath.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StudentClass {

  private String name;
  private List<String> subjects = new ArrayList<String>();
  private List<Student> students = new ArrayList<Student>();
  private Map<String, List<Student>> subjectStudentMap = new HashMap<String, List<Student>>();
  
  public StudentClass(String name) {
    super();
    this.name = name;
  }

  public String getName() {
    return name;
  }
  
  public void setName(String name) {
    this.name = name;
  }
  
  public List<String> getSubjects() {
    return subjects;
  }
  
  public void setSubjects(List<String> subjects) {
    this.subjects = subjects;
  }
  
  public List<Student> getStudents() {
    return students;
  }
  
  public void setStudents(List<Student> students) {
    this.students = students;
  }

  public Map<String, List<Student>> getSubjectStudentMap() {
    return subjectStudentMap;
  }

  public void setSubjectStudentMap(Map<String, List<Student>> subjectStudentMap) {
    this.subjectStudentMap = subjectStudentMap;
  }
  
}
   

File Name  :  
com/bethecoder/tutorials/jxpath/RelativeContextTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.jxpath;

import java.util.Arrays;

import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.Pointer;

import com.bethecoder.tutorials.jxpath.common.Student;
import com.bethecoder.tutorials.jxpath.common.StudentClass;

public class RelativeContextTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    
    Student std1 = new Student("Sriram"2"Chess"new String [] { "Sri""Ram""Bunny""Sweety""Honey"});
    Student std2 = new Student("Sudhakar"29"Cricket"new String [] { "Venkat""Munna""Sudha" });
    Student std3 = new Student("Anuradha"2"Cooking"new String [] { "Anu""Radha" });
    
    StudentClass stCls = new StudentClass("MPC");
    stCls.setStudents(Arrays.asList(std1, std2, std3));

    //Create JXPathContext with StudentClass instance as ROOT node
      JXPathContext context = JXPathContext.newContext(stCls);
      Pointer sriramPointer = context.getPointer("/students[name='Sriram']");
      System.out.println(sriramPointer);
      
      //Create relative context with reference to the given pointer
      JXPathContext relativeContext = context.getRelativeContext(sriramPointer);
      System.out.println(relativeContext.getValue("."))//Current node which is 'Student' object
      System.out.println(relativeContext.getValue("./age"))//Get Student's age
      System.out.println(relativeContext.getValue("./hobby"))//Get Student's hobby
      System.out.println(relativeContext.getValue("./nickNames"))//Get Student's nick names

  }
}
   

It gives the following output,
/students[1]

Student[name = Sriram, age = 2, hobby = Chess, [Sri, Ram, Bunny, Sweety, Honey]]
2
Chess
[Sri, Ram, Bunny, Sweety, Honey]



 
  


  
bl  br