Use XPath to obtain specific HTML tags

Source: Internet
Author: User
Package nekohtml; import Java. io. ioexception; import javax. XML. transform. transformerexception; import Org. apache. XPath. xpathapi; import org.cyberneko.html. parsers. domparser; import Org. w3C. dom. document; import Org. w3C. dom. nodelist; import Org. XML. sax. saxexception; public class nekohtmlandxpath {// resolve the corresponding HTML to the DOM document public static document getdocument (string URL) {domparser parser = new domparser (); try {Parser. parse (URL);} catch (saxexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} document DOC = parser. getdocument (); Return Doc;} // use XPath to locate the specific node public static nodelist getexactnode (document DOC, string XP) {nodelist list = NULL; try {list = xpathapi. selectnodelist (Doc, XP);} catch (transformerexception e) {e. printstacktrace ();} return list;} public static void main (strin G [] ARGs) {string Baidu = "http://www.baidu.com /"; string bpath = "// html // body // Div [2] // P // map // area"; string yyt = "http://www.yinyuetai.com/mv/all? Page = 1 "; string ypath = "/html/body/Div [6]/Div [2]/Div [2]/Div [3]/ul/Li/Div [2]/ h3/"; // resolve the specified page to the DOM document DOC = getdocument (yyt); // obtain the specified node nodelist list = getexactnode (Doc, ypath) based on XPath; system. out. println ("the number of nodes meeting the condition is:" + list. getlength (); For (INT I = 0; I <list. getlength (); I ++) {system. out. println ("the obtained node attribute is:" + list. item (I ). getattributes (). getnameditem ("title "));}}}

Jar package used: xalan. jar; xercesimpl. jar; nekohtml. jar.

The XPath for obtaining tags uses firebug, which sometimes requires some conversion. Because firebug converts HTML into a standard DOM, there may be very few small issues with XPath. For example, the tbody tag may be added. Just make a slight change.

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.