Menu

rebuilding html from tree

Help
2004-07-28
2013-05-15
  • Pino Gargiulo

    Pino Gargiulo - 2004-07-28

    Hi there.
    What's the right way to rebuild html from the parse tree?
    The tag name and his content comes from Node.tagName and Node.text, but the attributes (possibly in the right order) and all the remainings (if there were)?
    Thanks

     
    • Rich

      Rich - 2008-07-28

      It's taken me a while to get to the bottom of this one.  The documentation on tree.hh is quite good for working out how to solve the problem.  All it needs is a function to recursively parse the sub nodes of the tree out:

      This should get you going:

      tr = parser.getTree();
         
      tree<HTML::Node>::sibling_iterator sib = tr.begin();

      parseNode(&sib, 1);

      int parseNode(tree<HTML::Node>::sibling_iterator *thisnode, int level)
      {

          tree<HTML::Node>::sibling_iterator sib = thisnode->begin();
          while(sib != thisnode->end())
          {

              for(int y=0; y<level; y++) cout << "-";
              cout << "'" << sib->text() << "'" << endl;
             
              if(sib->isTag()) parseNode(&sib, level+1);
             
              for(int y=0; y<level; y++) cout << "-";
              cout << "'" << sib->closingText() << "'" << endl;
             
              ++sib;
             
          }

      }

      Hope this helps somebody

       

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.