Validate opening and closing of html tags

P

Pradeep

Hi,

Can anyone help me in solving this problem.
I have an example input:
sometext<b><i>some text</i></b>
the input may vary i.e. 1 tag is opened & not closed, some mismatches

To do:
1.check for few html tags like b,i,u
2.opening and closing of tags must be in proper order without
overlaping.

I have to write a java code to validate this.
Can anyone help me..

Thanks in Advance..

Regards,
Pradeep.
 
M

Mark Thomas

Pradeep said:
Hi,

Can anyone help me in solving this problem.
I have an example input:
sometext<b><i>some text</i></b>
the input may vary i.e. 1 tag is opened & not closed, some mismatches

To do:
1.check for few html tags like b,i,u
2.opening and closing of tags must be in proper order without
overlaping.

I have to write a java code to validate this.
Can anyone help me..

Thanks in Advance..

Regards,
Pradeep.
I'd use a finite state machine - googling that might get you started.

Mark
 
V

Venkatesh

U can just make use of stack and java pattern matching package
(java.util.regex) ....

Here is the code to find tags in given html string:

private static final String HTML_TAG_PATTERN = "<[^>]*>";
private static final Pattern searchPattern =
Pattern.compile(HTML_TAG_PATTERN);

private Matcher m = null;
private String m_htmlStr = null;

private boolean m_initDone = false;

public void init(String htmlStr){

m_htmlStr = htmlStr;
m = searchPattern.matcher(m_htmlStr);

m_initDone = true;

}

private String getNextTag() throws Exception {

if (!m_initDone) {
throw new Exception("Not yet initialized ....");
}

String tagToReturn = null;
if (m.find()) {
tagToReturn = m_htmlStr.substring(m.start(), m.end());
}
return tagToReturn;

}

So, make use of a stack and push all the start tags and selectively pop
them up whenever u find an end tag and compare to find if the start and
end tags match.

Hope this helps

-Venkatesh
 
G

Greg R. Broderick

[posted and mailed]

To do:
1.check for few html tags like b,i,u
2.opening and closing of tags must be in proper order without
overlaping.

I have to write a java code to validate this.
Can anyone help me..

Use a stack data structure.

Scan through the text looking for HTML tags.

When you encounter a start tag, push it on the stack.

When you encounter an end tag, pop the top element from the stack and
compare it to the end tag.

Cheers
GRB


--
---------------------------------------------------------------------
Greg R. Broderick [rot13] (e-mail address removed)

A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------
 
O

Oliver Wong

Pradeep said:
Hi,

Can anyone help me in solving this problem.
I have an example input:
sometext<b><i>some text</i></b>
the input may vary i.e. 1 tag is opened & not closed, some mismatches

To do:
1.check for few html tags like b,i,u
2.opening and closing of tags must be in proper order without
overlaping.

I have to write a java code to validate this.
Can anyone help me..

You might be interested in HTML Tidy:
http://www.w3.org/People/Raggett/tidy/

- Oliver
 
T

Tim Smith

I'd use a finite state machine - googling that might get you started.

Wait a second...isn't checking for closing tags being in the right order
and for tags not overlapping equivalent to the problem of recognizing
palindromes? And isn't that one of the classic examples of something
that you can't do with a finite state machine?
 
O

Oliver Wong

Tim Smith said:
Wait a second...isn't checking for closing tags being in the right order
and for tags not overlapping equivalent to the problem of recognizing
palindromes? And isn't that one of the classic examples of something
that you can't do with a finite state machine?

You're right. It can't be done with a finite state machine. You'd need
an infinite state machine (or a stack machine, or something equally
powerful, etc.)

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top