i have a Element object its a HTMLDocument object and i want to string value of this element.
i want this result
Christina Toth, Pharm. D.===============
Try this instead.
Edited to use the read() method of HTMLEditorKit.
import java.io.StringReader;
import javax.swing.text.AttributeSet;
import javax.swing.text.Element;
import javax.swing.text.ElementIterator;
import javax.swing.text.StyleConstants;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
public class NewMain {
public static void main(String args[]) throws Exception {
HTMLEditorKit htmlKit = new HTMLEditorKit();
HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
htmlKit.read(new StringReader(text), htmlDoc, 0);
// Parse
ElementIterator iterator = new ElementIterator(htmlDoc);
Element element;
while ((element = iterator.next()) != null) {
AttributeSet as = element.getAttributes();
Object name = as.getAttribute(StyleConstants.NameAttribute);
if (name == HTML.Tag.DIV) {
StringBuffer sb = new StringBuffer();
sb.append(name).append(": ");
int count = element.getElementCount();
for (int i = 0; i < count; i++) {
Element child = element.getElement(i);
int startOffset = child.getStartOffset();
int endOffset = child.getEndOffset();
int length = endOffset - startOffset;
sb.append(htmlDoc.getText(startOffset, length));
}
System.out.println(sb);
}
}
}
private static String text
= "\n"
+ "\n"
+ "pg_0001 \n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "Christina Toth, Pharm. D. \n"
+ "\n"
+ "\n"
+ "\n"
+ "";
}
Console:
div: Christina Toth, Pharm. D.