xpath-select.xtp

来自「解压在c盘」· XTP 代码 · 共 52 行

XTP
52
字号
<s1 title="XPath select()"><p>Finding a node is useful but often you will want to select all nodesmatching a pattern.  For example, a web spider will want to select all linksin a HTML page.</p><p>The <var/select()/> call returns an Iterator letting you look at allthe nodes.  The Iterator returns nodes in document order.</p><p>You can precompile XPath patterns and then reuse the precompiled patternover again.  The <var/XPath.find()/> call in the previous page used aconvenient, but inefficient call to find the node.  In this example, we'llprecompile the pattern before using the select.</p><p>The following example reads the home page of http://localhost:8080 andand returns all the &lt;a href> links in that page.</p><example title="Spidering a Web Page">Pattern pattern = XPath.parseMatch("a[@href]");Document doc = new Html().parseDocument("http://localhost:8080");Iterator iter = pattern.select(doc);while (iter.hasNext()) {  Element elt = (Element) iter.next();  System.out.println("link: " + elt.getAttribute("href"));}</example><results>link: /index.xtplink: /ref/faq.xtplink: /ref/index.xtplink: /javadoc/index.html...</results><s2 title="Summary"><ul><li>Precompiling <var/XPath/> patterns is more efficient<li><var/XPath.select()/> can "spider" web pages<li>select returns nodes in <var/document order/><li>The pattern <var/a[@href]/> returns <var/&lt;a>/> elementswith an <var/href/> attribute.</ul></s2></s1>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?