tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > JXPath > Count Function

Count Function 

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 using count function 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/CountFunctionTest.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 CountFunctionTest {

  /**
   @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);
      
      //Get students having more than 2 nick names
      Iterator<?> students = context.iterate("/students[count(nickNames) > 2]");
      while (students.hasNext()) {
        System.out.println(students.next());
      }
      
      //Get students having 4 or less nick names
      Iterator<?> students2 = context.iterate("/students[count(nickNames) <= 4]");
      while (students2.hasNext()) {
        System.out.println(students2.next());
      }
  }
}
   

It gives the following output,
Student[name = Sriram, age = 2, hobby = Chess, [Sri, Ram, Bunny, Sweety, Honey]]
Student[name = Sudhakar, age = 29, hobby = Cricket, [Venkat, Munna, Sudha]]

Student[name = Sudhakar, age = 29, hobby = Cricket, [Venkat, Munna, Sudha]]
Student[name = Anuradha, age = 2, hobby = Cooking, [Anu, Radha]]



 
  


  
bl  br