Java HTML Jsoup Element getElementByXPath(String xpath, Document doc)

Here you can find the source of getElementByXPath(String xpath, Document doc)

Description

get Element By X Path

License

Open Source License

Declaration

public static Element getElementByXPath(String xpath, Document doc) 

Method Source Code

//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;
    }
}

Related

  1. getAllNodes(Element element)
  2. getAuthorName(Element authorElement)
  3. getCodeSnippetCLT(Element codeElement, String content, String fqn, String api, String kind, String[] titles)
  4. getDate(Elements caption)
  5. getDouble(Elements line, int idx)
  6. getElementsByClass(String className, Element boardElement)
  7. getElementsFirstLevel(String html)
  8. getElementTextBySelector(String html, String selector)
  9. getElementValue(Element wrapperElement, String cssQuery)