I want to parse an HTML file in order to get the background property in the body tag and src property of the Frame tag. Here is the sample program I tried, but I didn't get.
**************************************
import java.util.Iterator;
import java.util.LinkedList;
public class LSParserVisitor extends NodeVisitor {
private LinkedList listOfLinks;
{
listOfLinks = new LinkedList();
}
public LSParserVisitor() {
}
public void visitTag(BodyTag bodyTag){
listOfLinks.add(bodyTag);
}
public void visitTag(FrameTag frameTag){
listOfLinks.add(frameTag);
}
public LinkedList getListOfLinks() {
return listOfLinks;
}
public static void main(String[] args) {
try {
LSParserVisitor parserVisitor = new LSParserVisitor();
Parser parser = new Parser(
"F:\\vinoth\\docs\\api\\index.html");
parser.visitAllNodesWith(parserVisitor);
Iterator itr = parserVisitor.getListOfLinks().iterator();
while (itr.hasNext()) {
Object ob=itr.next();
if(ob instanceof BodyTag){
System.out.println("Body Tag "+((BodyTag)ob).getAttribute("Background"));
}else if(ob instanceof FrameTag){
System.out.println("Frame Tag "+((FrameTag)ob).getAttribute("src"));
}
}
} catch (ParserException pe) {
System.out.println("ParserException" + pe.getMessage());
}
}
}
************************************
I don't know what is the problem with this code.
Could somebody help me and give me a solution for this problem.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You'll need to override the visitTag(Tag x) method of NodeVisitor. The signatures you have don't match any of the methods in NodeVisitor. Check for the tag's name matching those you're interested in:
LSParserVisitor...
public void visitTag (Tag tag)
{
if (tag.getName ().equals ("BODY"))
listOfLinks.add (tag);
else if (tag.getName ().equals ("FRAME"))
listOfLinks.add (tag);
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I want to parse an HTML file in order to get the background property in the body tag and src property of the Frame tag. Here is the sample program I tried, but I didn't get.
**************************************
import java.util.Iterator;
import java.util.LinkedList;
import org.htmlparser.Parser;
import org.htmlparser.tags.*;
import org.htmlparser.util.ParserException;
import org.htmlparser.visitors.NodeVisitor;
public class LSParserVisitor extends NodeVisitor {
private LinkedList listOfLinks;
{
listOfLinks = new LinkedList();
}
public LSParserVisitor() {
}
public void visitTag(BodyTag bodyTag){
listOfLinks.add(bodyTag);
}
public void visitTag(FrameTag frameTag){
listOfLinks.add(frameTag);
}
public LinkedList getListOfLinks() {
return listOfLinks;
}
public static void main(String[] args) {
try {
LSParserVisitor parserVisitor = new LSParserVisitor();
Parser parser = new Parser(
"F:\\vinoth\\docs\\api\\index.html");
parser.visitAllNodesWith(parserVisitor);
Iterator itr = parserVisitor.getListOfLinks().iterator();
while (itr.hasNext()) {
Object ob=itr.next();
if(ob instanceof BodyTag){
System.out.println("Body Tag "+((BodyTag)ob).getAttribute("Background"));
}else if(ob instanceof FrameTag){
System.out.println("Frame Tag "+((FrameTag)ob).getAttribute("src"));
}
}
} catch (ParserException pe) {
System.out.println("ParserException" + pe.getMessage());
}
}
}
************************************
I don't know what is the problem with this code.
Could somebody help me and give me a solution for this problem.
You'll need to override the visitTag(Tag x) method of NodeVisitor. The signatures you have don't match any of the methods in NodeVisitor. Check for the tag's name matching those you're interested in:
LSParserVisitor...
public void visitTag (Tag tag)
{
if (tag.getName ().equals ("BODY"))
listOfLinks.add (tag);
else if (tag.getName ().equals ("FRAME"))
listOfLinks.add (tag);
}