Solved

Java code to detect the HTML tag <P>

Posted on 2002-05-03
11
410 Views
Last Modified: 2012-06-27
Is there any method in Java to detect the HTML tag for a articles with tag. For example, i have 2 paragraph here. i need to have a function that can return each of the paragraph. Actually i want to put a picture beside each paragraph at the next page. And there were be more than 2 paragraph. And in between will have other HTML tag like <Strong>, <BR>, etc...

Example:
<P><B>Genting Highlands</B> Resort offers the bountiful harvest of nature together with a staggering potpourri of international standard facilities. Besides the cool air and scenic surroundings, this hilltop city also houses the country's one and only casino.<BR></P>
<P>However, one should not underestimate its value as this casino has been featured in many films produced by Hong Kong, Taiwan, and even Hollywood. Besides the casino, this hilltop resort also has a theme park of its own and an entertainment center.<BR></P>

Thanks in advance.

rgds,
joe
0
Comment
Question by:jo_e
  • 4
  • 2
  • 2
  • +2
11 Comments
 

Expert Comment

by:stephendl
ID: 6988161
You need something along the lines of this...

String text = (your text to be searched);
String searchString = "</P>";
String insertString = "<img src=\"my image url\">";
int oldPosition = 0;
int position = text.indexOf(searchString);

while (position != -1) {
  // insert the string into the text
  text = text.substring(oldPosition, position) +
     insertString  +
     text.substring(position, text.length());
  // set the old position to the current position plus the
      length of the string just added and the length of the
      string searched for (so we don't find it again!)
  oldPosition = position +
     searchString.length() +
     insertString.length();
  // search for the string again
  position = text.indexOf(searchString);
}

There are probably some errors with the exact indexes (0 based Vs 1 based indexes) so you might have to add or minus a 1 here or there. There are probably some special cases that you might have to look out for too. But this should give you a good base to start from. Hope it helps.

Stephen
0
 

Expert Comment

by:stephendl
ID: 6988180
Sorry, I forgot to point out, this is obviously VERY inefficent. It would be better / faster to use a StringBuffer to construct the new string into.
0
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6988200
Jo_e

I can give u a high level of doing it.
You can use XML Parsing or
use javax.swing.text.html/javax.swing.text.html.parser package which has lot of classes for doing have a look at documentation.

Thanks
Shyam
0
 
LVL 9

Accepted Solution

by:
Ovi earned 50 total points
ID: 6988267
Use the HTMLParser from swing, build a HTMLDocument object and traverse the structure and make changes that you want to do.

HTMLDocument doc = new HTMLDocument();
HTMLEditorKit.read(new FileReader("x.html"), doc, 0);

... and traverse the document for querying the <p> tag. The document structure is tree like one made up of Element objects. A Element object for HTMLDocument represents a html tag. To get the type of the tag you do something like :

AttributeSet attr = aElement.getAttributes();
if(attr.getAttribute(StyleConstants.NameAttribute).equals(HTML.Tag.P))
  System.out.println("Paragraph detected");
else
  System.out.println("Not a paragraph");

If you are sure that you have paragraps at only one level (as children of the BODY element), all you have to do is to search for the BODY element, retrieve it's children with Element's methods and test for paragraphs. If there are deeper paragraphs, you should traverse recursively the document tree structure for finding them.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6988290
To find a specific element, let's say BODY, beginning from the root of the structure you should do :

public Element getBodyElement(HTMLDocument doc) {
  Element root = doc.getRootEelements()[0];
  return(findChild(root));
}

public Element findChild(Element root, HTML.Tag type) {
  if(matchType(root, type))
    return(root);
  int count = root.getElementCount();
  for(int i = 0; i<count; i++) {
    Element child = findChild(root.getElement(i), type);
    if(child != null)
     return(child);
  }
  return(null);
}

public boolean matchType(Element e, HTML.Tag type) {  
return(e.getAttributes().getAttribute(StyleConstants.NameAttribute).equals(type));
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:Ovi
ID: 6988305
... and of course all the required imports :
import java.swing.text.*;
import java.swing.text.html.*;


Note : implementing in such maneer give's you the possibility to extend your application not handlig only <p> tags, but all defined ones.
0
 

Author Comment

by:jo_e
ID: 6990762
Hi Ovi,

Thanks for your kindly answer my question.
But the wway can i use this in the JSP? Because i am not use the swing to built the interface.

<import java.swing.text.*;
import java.swing.text.html.*;>

Thank you.

joe

0
 

Author Comment

by:jo_e
ID: 6990770
Hi Ovi,

Thanks for your kindly answer my question.
But the wway can i use this in the JSP? Because i am not use the swing to built the interface.

<import java.swing.text.*;
import java.swing.text.html.*;>

Thank you.

joe

0
 
LVL 9

Expert Comment

by:Ovi
ID: 6995629
If you can communicate with a servlet for this purpose, yes.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8709675
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to Ovi

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What is the latest versions eclipse neon 2 141
thymeleaf natural templating vs JSP 2 69
javap not working 8 39
Way to decrease size of apk file 9 46
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now