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

How to Iterate JXPath Pointer 

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 how to iterate node Pointers in JXPath.

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/IteratingPointersTest.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 org.apache.commons.jxpath.Pointer;

import com.bethecoder.tutorials.jxpath.common.Author;
import com.bethecoder.tutorials.jxpath.common.Book;
import com.bethecoder.tutorials.jxpath.common.BookStore;

public class IteratingPointersTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    BookStore bookStore = new BookStore(
      Arrays.asList(
        new Book("A-Book1"11.11new Author("A")),
        new Book("B-Book1"22.22new Author("B")),
        new Book("B-Book2"33.33new Author("B")),
        new Book("C-Book1"33.33new Author("C")),
        new Book("C-Book2"16.66new Author("C")),
        new Book("C-Book3"48.12new Author("C")),
        new Book("D-Book1"44.44new Author("D"))
      )
    );

    //Create JXPathContext with BookStore instance as ROOT node
      JXPathContext context = JXPathContext.newContext(bookStore);
      
      //Get all book titles by author 'C'
      Iterator<?> bookNamesByCPointer = context.iteratePointers("/books/author[name='C']");
     
      Pointer pointer = null;
      JXPathContext authorContext = null;
      while (bookNamesByCPointer.hasNext()) {
        pointer = (PointerbookNamesByCPointer.next();
        authorContext = context.getRelativeContext(pointer);
        System.out.println(authorContext.getValue("../title"));
      }

      //Get all book titles by author 'B'
      Iterator<?> bookNamesByBPointer = context.iteratePointers("/books/author[name='B']");

      while (bookNamesByBPointer.hasNext()) {
        pointer = (PointerbookNamesByBPointer.next();
        authorContext = context.getRelativeContext(pointer);
        System.out.println(authorContext.getValue("../title"));
      }

  }

}
   

It gives the following output,
C-Book1
C-Book2
C-Book3

B-Book1
B-Book2



 
  


  
bl  br