Menu

Heading tags: h1..h6, how?

Help
2004-12-27
2013-04-27
  • Peter Veentjer

    Peter Veentjer - 2004-12-27

    How can I detect if h1..h6 tags are read? I`m currently writing a searchengine (on top of lucine) and I need to access those fields. The reason I need to know if the text is within a h1..h6 tag, is because those texts are more important (usually) and the searchengine can adjust the searchresult.

     
    • Derrick Oswald

      Derrick Oswald - 2004-12-27

      If you have the text node and want to find if it's enclosed in a <H1></H1> tag, you could walk up the parent chain checking for the H1 tag, something like.

      Tag tag;  // this is the tag you are checking
      boolean inH1;

      inH1 = false;
      while (!inH1 && (null != tag.getParent()))
          if ("H1".equals (tag.getParent().getTagName()))
              inH1 = true;
          else
              tag = tag.getParent();

      If you haven't got the tag already and want to get everything enclosed in a <H1></H1> tag, use a filter, something like:

          NodeList list = parser.extractAllNodesThatMatch(new HasParentFilter (new TagNameFilter("H1")));

       

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.