Menu

Get the content after make a change ?

Help
grayworld
2006-02-20
2013-04-27
  • grayworld

    grayworld - 2006-02-20

    Here's my code :
    After i used a NodeVisitor to modify the content , i have done as following to get the content and print to console or do something else . but it returned nothing ! can any body tell me why ?

    My idea is to process a html file , change the "src" attributes of the <img> tag and return a String contains the document after modified .I just haven't known which is the easiest way to do it . Cound any one give me an advice ?

    thanks in advance !

    public class MyVisitor extends NodeVisitor
    {
        public MyVisitor ()
        {
        }

        public void visitTag (Tag tag)
        {
             if (tag.getTagName().equals ("img") || tag.getTagName().equals ("IMG")){
                 String imgPath = tag.getAttribute("src") ;
                 imgPath = "something" + imgPath  ;
                 tag.setAttribute("src" ,imgPath );
               
                                                  
             }
         //   System.out.println ("\n" + tag.getTagName () + tag.getStartPosition ());
        }

        public void visitStringNode (Text string)
        {
          //   System.out.println (string);
        }

        public static void main (String[] args) throws ParserException
        {
            Parser parser = new Parser ("c:/index.html");
            NodeVisitor visitor = new MyVisitor ();
            parser.visitAllNodesWith (visitor);
           
            NodeList list = new NodeList ();       
            NodeIterator iterator =  parser.elements();
           
            while (iterator.hasMoreNodes ())
                list.add (iterator.nextNode()); // ok got the node list
           
           
            System.out.println("list :\n" + list.toHtml()) ; // print nothing !
           
        }
    }

     
    • grayworld

      grayworld - 2006-02-20

      Oh ,I got it .

      I need to gather all the nodes first, then apply the filter and process it and then print out the full list:
      NodeList all = parser.Parse (null);
      Nodelist imgNodeList = all.extractAllNodesThatMatch (imgFilter);
      // ... processing as above
      System.out.println (all.toHtml ());

      Thanks Derrick Oswald so much for the solution .

       

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.