help me to validate a html file..to check for tag matching nd nesting..

i need to write a program to open a given file and see if the nesting is fine,
assuming that all the tags in the html string are correctly formed,
that is when an angle bracket is seen,
assuming that it is followed by the remainder of a complete tag.

basically like validating infix expression..
but i dn ve a clear idea..
help me out!!

my prob is in pushing the html tag onto the stack..
In start tag, once i find '<' i need to store the characters as a string till it reaches '>'..
so that i ll be able to match d string wth d end tag..
dono hw to extract it..

i dn get u..
if i do so,wont i be pushing as characters into stack..
i wanna extract as substring..
like..

<html>
<body>
sdfg
</body>
</html>

In this code,
once i reach '<' in <html>,i need to store "html" as a string..
again wen i reach <body>,push "body" again as a string..
and wen i reach '</',in </body>,i pop d earlier n check whether both r same..