In a class that extends NodeVisitor is there an easier way for the
public void visitTag (Tag tag)
{
}
to obtain the text between this tag and the end tag?
The solution that i found right now is to have a variable that is set to true in the visitTag and to false in public void visitEndTag(Tag tag) { ..}
and also implement
public void visitStringNode(Text text) {
if(variable == true)
{ //consider that the text is beetween the start and end tag
}
}
Is this a good aproach? The getText() method of Tag object returns the tag and attributes, so it is not sufficient for me. Just curios is my way is a good way to solve this issue.
Thanks.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I found a better solution in that i cast to a CompositeTag - the text was bettew <tr> <th> and <td>s and use getChildrenAsNodeArray() and on the nodes toPlainTextString()
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
When the NodeVisitor visitTag() is called, the tag has been completely parsed, so it should just be a matter of running over the NodeList from getChildren(), if any, with a StringBean. This is possible because StringBean also implements NodeVisitor.
So, in your visitTag() method, do something like this:
StringBean sb = new StringBean ();
NodeList list = tag.getChildren ();
if (null != list)
list.visitAllNodeWith (sb);
System.out.println (sb.getStrings ());
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
In a class that extends NodeVisitor is there an easier way for the
public void visitTag (Tag tag)
{
}
to obtain the text between this tag and the end tag?
The solution that i found right now is to have a variable that is set to true in the visitTag and to false in public void visitEndTag(Tag tag) { ..}
and also implement
public void visitStringNode(Text text) {
if(variable == true)
{ //consider that the text is beetween the start and end tag
}
}
Is this a good aproach? The getText() method of Tag object returns the tag and attributes, so it is not sufficient for me. Just curios is my way is a good way to solve this issue.
Thanks.
I found a better solution in that i cast to a CompositeTag - the text was bettew <tr> <th> and <td>s and use getChildrenAsNodeArray() and on the nodes toPlainTextString()
When the NodeVisitor visitTag() is called, the tag has been completely parsed, so it should just be a matter of running over the NodeList from getChildren(), if any, with a StringBean. This is possible because StringBean also implements NodeVisitor.
So, in your visitTag() method, do something like this:
StringBean sb = new StringBean ();
NodeList list = tag.getChildren ();
if (null != list)
list.visitAllNodeWith (sb);
System.out.println (sb.getStrings ());