Here you can find the source of getElementByXPath(String xpath, Document doc)
public static Element getElementByXPath(String xpath, Document doc)
//package com.java2s; //License from project: Open Source License import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; public class Main { public static Element getElementByXPath(String xpath, Document doc) { String[] tags = xpath.substring(11, xpath.length()).split("/"); // ommiting /html/body/ Element clt = doc.body(); int index; int divc = 0; boolean done = false; for (String tag : tags) { if (tag.startsWith("div")) divc++;// ww w .java 2 s. c o m index = 0; int bindex = tag.indexOf('['); int eindex = tag.indexOf(']'); if (bindex != -1) { index = Integer.parseInt(tag.substring(bindex + 1, eindex)) - 1; tag = tag.substring(0, bindex); } try { clt = clt.select(">" + tag).get(index); } catch (IndexOutOfBoundsException ex) { System.out.println(xpath + " is not valid."); clt = null; break; } if (tag.equals("table")) clt = clt.select(">tbody").get(0); } return clt; } }