Menu

obtaining parent of unusual TextNode

Help
wbrian
2007-11-14
2013-04-27
  • wbrian

    wbrian - 2007-11-14

    my app is to find a given input string in a html file and then set its background colour to yellow.

    I've created a visitor and am able to find the TextNodes of input string right fine.

    the alogrithm for turning on the background I thought would be:
    for each TextNode (that forms a part of the input string)
       split the text into: "before", "belongs to", "after" the part that belongs to the input string.
       for each "belongs to"
         create a new span as its new parent
         set the background colour
         set the parent of the span to the original parent of the "belongs to"

    the problem I've having is that for text wrapped in a <b> tag the parent of the TextNode is not the <b> but the <span> that is the parent of the <b>.

    for the ex.: <span id="p3_10"><br><b id="p3_12">Thursday August 30, 2007</b></span>

    the result of: 
       Node parentNode = ((TextNode)n).getParent();
       String id = ((Tag)parentNode).getAttribute("id");

    is:

    TEXT i = 50  Thursday August 30, 2007
    PARENT IS A TAG   i = 50  PARENT ID = p3_10 PARENT NAME = SPAN
    TAG  i = 51  B
    PARENT IS A TAG   i = 51  PARENT ID = p3_10 PARENT NAME = SPAN

    in javascript the parent of the text is recognized as the <b> tag.

    I was wondering if you had any suggestions as to obtaining recognition of the immediate parent or if that is not possible a work around.

    thank you for any assistance that you might be able to provide

    Brian
      

     
    • wbrian

      wbrian - 2007-11-14

      Hi there,

      an addendum:
      the TextNode "Thursday ... "  is listed among the children of the <span> tag,
      the <br> tag and the <b> tag.

      thank you again,
      Brian

       
    • Derrick Oswald

      Derrick Oswald - 2007-11-15

      The FAQ article http://htmlparser.sourceforge.net/faq.html#composite shows how to make <B> tags composite so that it will have the text nested inside it.

      Or failing that, try walking backwards up the children list from the text node of interest, looking for TagNode objects with an ID attribute.

       

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.