The moose likes Java in General and the fly likes xml tags verification Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "xml tags verification" Watch "xml tags verification" New topic
Author

xml tags verification

smitha rajgopal
Greenhorn

Joined: Sep 22, 2005
Posts: 14
Hello,
I have to write a java program,which checks for proper closing of xml tag and if the tags are not closed properly,program needs to close it in correct order.
for ex: <b><font>mmmm</b></font>

I need to change this string to <b><font>mmmm</font></b>.

please help,any sample code will help me moore.

Thanks,
Smitha
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Do you really need to do this, or do you just need to parse some HTML? There are already some good HTML parsers out there (you might even be able to use them to then save the parse tree as correct XHTML).


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
smitha rajgopal
Greenhorn

Joined: Sep 22, 2005
Posts: 14
Thanks for your reply,
I need to such manipulation on string.

Any help,how can i do that?
Jaap Vermeer
Greenhorn

Joined: Apr 04, 2006
Posts: 16
Are you allowed to assume that the source is valid xml? If so, use an XML parser and SAX or DOM through it.
If you just go through the xml in a "linear" way, you can keep a stack of open tags and check that if the one currently closed equals the last opened.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jaap Vermeer:
Are you allowed to assume that the source is valid xml?


If the tags aren't closed properly, it cannot be valid xml, I'd think...
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by smitha rajgopal:
Thanks for your reply,
I need to such manipulation on string.

Any help,how can i do that?


I'd use a HTML parser, like NekoHTML or JTidy (http://java-source.net/open-source/html-parsers) to parse the string, and then write the DOM tree back as valid XML.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: xml tags verification
 
Similar Threads
Soap Error.
Parsing an XML
pulling data from xml using javascript
The program error!
Removing HTML tags