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

How to Access Maps using JXPath 

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 accessing maps using 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/AccessMapsTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.jxpath;

import java.util.Arrays;
import java.util.Iterator;

import org.apache.commons.jxpath.JXPathContext;

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

public class AccessMapsTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    Student std1 = new Student("Sriram"2"Chess");
    Student std2 = new Student("Sudhakar"29"Cricket");
    Student std3 = new Student("Anu"2"Cooking");
    
    StudentClass stCls = new StudentClass("MPC");
    stCls.getSubjectStudentMap().put("MATHS", Arrays.asList(std1, std2));
    stCls.getSubjectStudentMap().put("PHYSICS", Arrays.asList(std2, std3));
    stCls.getSubjectStudentMap().put("CHEMISTRY", Arrays.asList(std3, std1));

    //Create JXPathContext with StudentClass object as root node
      JXPathContext context = JXPathContext.newContext(stCls);
      
      //Get the names of all MATHS Students
      Iterator<?> mathStdNames = context.iterate("/subjectStudentMap/MATHS/name");
      while (mathStdNames.hasNext()) {
        System.out.println(mathStdNames.next());
      }
      
      //Alternative Syntax
      //Get the names of all PHYSICS Students
      Iterator<?> phyStdNames = context.iterate("/subjectStudentMap[@name='PHYSICS']/name");
      while (phyStdNames.hasNext()) {
        System.out.println(phyStdNames.next());
      }
  }
}
   

It gives the following output,
Sriram
Sudhakar

Sudhakar
Anu



 
  


  
bl  br