Menu

Obtaining text between tags

Help
2006-10-10
2013-04-27
  • Balamaci Serban

    Balamaci Serban - 2006-10-10

    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.

     
    • Balamaci Serban

      Balamaci Serban - 2006-10-11

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

       
    • Derrick Oswald

      Derrick Oswald - 2006-10-12

      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 ());

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.