Solved

Java code to detect the HTML tag <P>

Posted on 2002-05-03
11
407 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

706 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

18 Experts available now in Live!

Get 1:1 Help Now