Learn More
Learn More

Reputation: 1561

Highlight nodes covered by XPath

I want to highlight nodes covered by a given XPath in an HTML page source.

I looked to in HtmlUnit, could not find any thing in API.

At present, I am thinking of doing it following way:

  1. Get XPath and Get Page-source.
  2. Break XPath to smaller chunks and store them in Xpath_chunk[].
  3. Apply chunk from Xpath_chunk[] starting from 0.
  4. Update css property of each node found by each chunk(any change which distinguishes the text).
  5. Now add get next chunk and concate it with current.
  6. Go to Step 3.

In this way, I will visit all nodes covered by Xpath. With some more changes I can remove those elements which were unnecessarily highlighted.

This is too complicated. Is there a way to do this ?

Upvotes: 1

Views: 364

Answers (1)

Joachim Nielandt
Joachim Nielandt

Reputation: 155

I recently made a rudimentary implementation using JavaFX's WebEngine + WebView. Register a DOM listener to all the nodes of the loaded website's Document, so you can listen to clicks on those Nodes. When clicking, change the style of the Node and add some CSS. WebView will reflect the changes and render the page correctly.

The Document that WebEngine returns can also be accessed using XPath (it's a w3d document), so you can traverse it and modify all the nodes you encounter (or use the Node that is the furthest down, then travel upwards using getParent(), and modify each parent as well).

Upvotes: 1

Related Questions