Java Open source XPath-enabled HTML parser introduction--jsoupxpath

Source: Internet
Author: User

Jsoupxpath (Https://github.com/zhegexiaohuozi/JsoupXpath) is a purely Java-developed parser that uses XPath to parse and extract HTML content, and XPath parsing is completely independent of execution. The DOM tree generation of HTML is named Jsoupxpath with the help of Jsoup. In order to enjoy the power and convenience of XPath in Java, but also can not find a strong enough XPath parser, so developed the Jsoupxpath. Jsoupxpath's implementation logic is clear and easy to extend, supporting almost all common XPath syntax, such as the following:

1http//www.cnblogs.com/as an example2"//a/@href";3"//div[@id = ' Paging_block ']/div/a[text () = ' Next > ']/@href";4"//div[@id = ' Paging_block ']/div/a[text () *= ' Next ']/@href";5"//h1/text ()";6"//h1/alltext ()";7"//h1//text ()";8"//DIV/A";9"//div[@id = ' post_list ']/div[position () <3]/div/h3/alltext ()";Ten"//div[@id = ' post_list ']/div[first ()]/div/h3/alltext ()"; One"//div[@id = ' post_list ']/div[1]/div/h3/alltext ()"; A"//div[@id = ' post_list ']/div[last ()]/div/h3/alltext ()"; - //find entries with a comment greater than 1000 (of course, just to demonstrate complex XPath, the predicate can be nested in a variety of ways, so as to test more comprehensive) -"//div[@id = ' post_list ']/div[./div/div/span[@class = ' Article_view ']/a/num () >1000]/div/h3/alltext ()"; the //Shaft Support -"//div[@id = ' post_list ']/div[self::d iv/div/div/span[@class = ' Article_view ']/a/num () >1000]/div/h3/alltext ()"; -"//div[@id = ' post_list ']/div[2]/div/p/preceding-sibling::h3/alltext ()"; -"//div[@id = ' post_list ']/div[2]/div/p/preceding-sibling::h3/alltext () |//div[@id = ' post_list ']/div[1]/div/h3/ Alltext () ";

There's no comparison between frames here, but I'm sure you'll find Jsoupxpath is the most powerful XPath parser on the market right now.

Quick Start

If it is not easy to use MAVEN, you can directly use the Lib under the dependency package to run to try, such as convenient to use the following dependency (has been uploaded to the central Maven Library, the latest version of 0.1.1):

1 <Dependency>2    <groupId>Cn.wanghaomiao</groupId>3    <Artifactid>Jsoupxpath</Artifactid>4    <version>0.1.1</version>5 </Dependency>

Once you've configured your dependencies, you can use the following example to experience it!

1String xpath= "//div[@id = ' post_list ']/div[./div/div/span[@class = ' Article_view ']/a/num () >1000]/div/h3/alltext ( )";2String doc = "...";3Jxdocument jxdocument =NewJxdocument (DOC);4List<object> rs =Jxdocument.sel (XPath);5  for(Object o:rs) {6     if(OinstanceofElement) {7         intindex =(Element) o). Siblingindex ();8 System.out.println (index);9     }Ten System.out.println (o.tostring ()); One}

Other examples can be referenced cn.wanghaomiao.example under the package

A grammar

Supports standard XPath syntax (supports predicate nesting), supports all common functions, supports all commonly used axes, and removes some of the standard flashy functions and axes, which are described below. Syntax can refer to http://www.w3school.com.cn/xpath/index.asp

Some considerations for using XPath

It is not recommended to paste directly in Firefox or Chrome generated XPath, these browsers in the rendering page will automatically complete some of the tags according to the standard, such as the table label will automatically add tbody tags, so that the resulting XPath path is not the most common, it is likely to not get the value. So, to use XPath and feel the power of XPath and the convenience and elegance that he brings, the best is to learn the standard syntax of XPath, so that you can handle all kinds of problems and enjoy the true power of XPath!

Two functions
    • text()Extract the own text of a node
    • node()Extracting all nodes
    • position()Returns the position of the current node in the sibling
    • last()Returns the last node in the sibling node
    • first()Returns the first node in a sibling node
Parser extension functions
    • allText()Extract all the text under the node instead of a //div/h3//text() recursive text usage like this
    • html()Gets the internal HTML for all nodes
    • outerHtml()Gets all the HTML that contains the node itself, including all nodes
    • num()Extract all the numbers in the node's own text, if you know that there is only one number in the node's own text (that is, non-descendant nodes contain text), such as reading, comments, prices and so directly can directly extract this number out. If there are multiple numbers, the first matched consecutive number is fetched.
Other Notes
    • contains(arga,argb)This function is temporarily unsupported because Jsoupxpath has a powerful operator support that can completely replace it! For example, you can use the *= substitution contains() examples://div[text()*=‘next‘]
Tri-axis
    • selfNode itself
    • parentParent node
    • childDirect child nodes
    • ancestorAll ancestor nodes father, Grandpa, Grandpa's father ...
    • ancestor-or-selfAll ancestor nodes and self nodes
    • descendantAll descendant nodes son, grandson, son of grandson ...
    • descendant-or-selfAll descendant nodes and themselves
    • preceding-siblingAll sibling nodes in front of the node
    • following-siblingAll sibling nodes behind the node
Shaft Utility Extensions
    • preceding-sibling-onePrevious sibling node (extended)
    • following-sibling-oneReturns the next sibling node (extension) syntax
    • siblingAll compatriots (expansion)
Four operators

These operators are sufficient to meet almost all of your needs.

    • |Returns a set of two node sets
    • a+bPlus returns a numeric result
    • a-bMinus returns a numeric result
    • a=bDetermines whether equality returns a Boolean
    • a!=bNot equal to return Boolean
    • a>bis greater than the return Boolean
    • a>=bGreater than or equal to return Boolean
    • a<bLess than return Boolean
    • a<=bLess than or equal to return Boolean
Operator extension
    • a^=bDoes string a start with string b a Startwith B returns a Boolean
    • a*=bWhether a contains B, a contains B returns a Boolean
    • a$=bWhether a Endwith B ends with a B returns a Boolean
    • a~=bWhether the content of a matches regular expression B returns a Boolean
Other Notes

Basic these are enough, other ribs temporarily do not support, if you have special needs please contact me.

Java Open source XPath-enabled HTML parser introduction--jsoupxpath

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.