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 Multiple Results

How to Access Multiple Results 

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 retrieving multiple results from JXPath expression using JXPathContext.iterate() API.

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

import java.util.List;

public class ComplexBean {

  private List<String> stringList;
  private String [] stringArray;
  
  public List<String> getStringList() {
    return stringList;
  }
  public void setStringList(List<String> stringList) {
    this.stringList = stringList;
  }
  public String[] getStringArray() {
    return stringArray;
  }
  public void setStringArray(String[] stringArray) {
    this.stringArray = stringArray;
  }

}
   

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

public class Book {
  
  private String title;
  private double price;
  private Author author;

  public Book(String title, double price, Author author) {
    super();
    this.title = title;
    this.price = price;
    this.author = author;
  }

  public String getTitle() {
    return title;
  }
  public void setTitle(String title) {
    this.title = title;
  }
  public double getPrice() {
    return price;
  }
  public void setPrice(double price) {
    this.price = price;
  }
  
  public String toString() {
    return "Book[" + title + "]";
  }

  public Author getAuthor() {
    return author;
  }

  public void setAuthor(Author author) {
    this.author = author;
  }
}
   

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

import java.util.List;

public class BookStore {
  
  private List<Book> books;
  
  public BookStore(List<Book> books) {
    super();
    this.books = books;
  }

  public List<Book> getBooks() {
    return books;
  }
  public void setBooks(List<Book> books) {
    this.books = books;
  }
}
   

File Name  :  
com/bethecoder/tutorials/jxpath/MultipleResultsTest.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.Author;
import com.bethecoder.tutorials.jxpath.common.Book;
import com.bethecoder.tutorials.jxpath.common.BookStore;
import com.bethecoder.tutorials.jxpath.common.ComplexBean;

public class MultipleResultsTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    ComplexBean bean = new ComplexBean();
    bean.setStringList(Arrays.asList("Maths""Physics""Chemistry""Hindi""English"));

    //Create JXPathContext with ComplexBean object as ROOT node
      JXPathContext context = JXPathContext.newContext(bean);
      
      //Iterate through first three values
      Iterator<?> firstThreeVals = context.iterate("/stringList[position() < 4]");
      while (firstThreeVals.hasNext()) {
        System.out.println(firstThreeVals.next());
      }
      
      //Iterate except first two values
      Iterator<?> exceptFirstTwoVals = context.iterate("/stringList[position() > 2]");
      while (exceptFirstTwoVals.hasNext()) {
        System.out.println(exceptFirstTwoVals.next());
      }
      
      
      BookStore bookStore = new BookStore(
          Arrays.asList(
              new Book("A1"11.11new Author("X1")),
              new Book("B2"22.22new Author("X2")),
              new Book("C3"33.33new Author("X3")),
              new Book("D4"44.44new Author("X4"))
          )
      );

      //Create JXPathContext with BookStore object as ROOT node
      JXPathContext bookContext = JXPathContext.newContext(bookStore);
      
      //Get the first two books
      Iterator<?> firstTwoBooks = bookContext.iterate("/books[position() < 3]");
      Book book = null;
      
      while (firstTwoBooks.hasNext()) {
        book = (BookfirstTwoBooks.next();
        System.out.println(book);
      }
      
  }

}
   

It gives the following output,
Maths
Physics
Chemistry

Chemistry
Hindi
English

Book[A1]
Book[B2]



 
  


  
bl  br