2017 © Pedro Pelรกez
 

library xpath-document

Friendlier XPath extension of DOMDocument for those fluent in beloved XPath!

image

wildhoney/xpath-document

Friendlier XPath extension of DOMDocument for those fluent in beloved XPath!

  • Wednesday, November 13, 2013
  • by Wildhoney
  • Repository
  • 1 Watchers
  • 0 Stars
  • 21 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

XPathDocument

, (*1)

Bower: bower install wildhoney/xpath-document, (*2)

Getting Started

XPathDocument allows you to chain your query methods, allowing you to delve deeper into the DOM hierarchy with each iteration., (*3)

$posts = $xpathDocument->query('//div[@class="posts"]');

foreach ($posts as $post) {
    $comments = $post->query('div[@class="comments"]');
}

Each query will return an instance of XPathDocument_Dom_List โ€“ and this class implements Iterator, ArrayAccess and Countable, which gives you lots of useful methods for manipulating the node collection., (*4)

, (*5)

Typically XPathDocument_Dom_List will hold a collection of XPathDocument_Dom_Element instances โ€“ but other instances are possible:, (*6)

  • XPathDocument_Dom_Element โ€“ generic elements with values and attributes;
  • XPathDocument_Dom_Attr โ€“ specific for node attributes;
  • XPathDocument_Dom_Text โ€“ specific for text values of nodes;

The latter two have a simple getText method for returning their values. However, XPathDocument_Dom_Element has the greatest flexibility., (*7)

Element Instance

With an instance of XPathDocument_Dom_Element you have the following methods:, (*8)

  • getText โ€“ retrieve the value of the node;
  • getHtml โ€“ retrieve the HTML value of the node;
  • getName โ€“ retrieve the name of the node (span, div, etc...);
  • getAttribute โ€“ retrieve an attribute by its name;
  • query โ€“ use node as the context for further querying;

Reddit Example

Please see the Reddit.com example in the example/index.php which will demonstrate how simple it is to crawl websites with XPathDocument!, (*9)

The Versions

13/11 2013

dev-master

9999999-dev

Friendlier XPath extension of DOMDocument for those fluent in beloved XPath!

  Sources   Download

by Adam Timberlake