Solved

Java code to detect the HTML tag <P>

Posted on 2002-05-03
11
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running JavaFX on JDeveloper 12C 1 116
What is the use of Forwarding Class in java 1 53
listing all the respondents to a twitter feed - Java 5 47
Java regex 5 21
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

739 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