Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse HTML and get CSS styles

I need to parse HTML and find corresponding CSS styles. I can parse HTML and CSS separataly, but I can't combine them. For example, I have an XHTML page like this:

<html>
<head>
<title></title>
</head>
<body>
<div class="abc">Hello World</div>
</body>
</html>

I have to search for "hello world" and find its class name, and after that I need to find its style from an external CSS file. Answers using Java, JavaScript, and PHP are all okay.

like image 281
atknatk Avatar asked May 13 '26 05:05

atknatk


1 Answers

Use jsoup library in java which is a HTML Parser. You can see for example here
For example you can do something like this:

String html="<<your html content>>";
Document doc = Jsoup.parse(html);
Element ele=doc.getElementsContainingOwnText("Hello World").first.clone(); //get tag containing Hello world
HashSet<String>class=ele.classNames(); //gives you the classnames of element containing Hello world

You can explore the library further to fit your needs.

like image 98
Narendra Rajput Avatar answered May 15 '26 17:05

Narendra Rajput