Re: [Htmlparser-user] how to add new nodes/elements to html source
Brought to you by:
derrickoswald
From: Srinivas V. <vip...@ho...> - 2007-06-27 06:26:07
|
Hi Derrick, Thanks very much for the response. I tried your suggestio= n and it seems to work. Can you please be precise about your comment "Like,= don't add a node after the current node in the parent's children.". Did yo= u mean something like this: TagNode tc =3D new Ta= bleColumn(); tc.setAttribute("id","testvalue",'"'); = tc.setAttribute("class","valid",'"'); nl.add(new TextNode("= Test3")); tc.setChildren(nl); tc.setEndTag(new Ta= bleColumn()); =0A= nl.add(tc); ---> this is the culprit System.ou= t.println(nl.toHtml()); --> This statement throws a stack overflow error.I= f that is the case then, where should my new nodes be stored. Should I crea= te another NodeList object which holds the original html and also my new ch= anges?Thanks,=0A= Srinivas.Date: Tue, 26 Jun 2007 19:27:15 -0700From: der...@ro...= mTo: htm...@li...Subject: Re: [Htmlparser-user] ho= w to add new nodes/elements to html source=0A= =0A= Srinivas,Try:TableColumn end =3D new TableColumn ();end.setTagName ("/TD");= You can modify the current html, but carefully so the visiting logic doesn'= t get into an infinite loop.Like, don't add a node after the current node i= n the parent's children.You will need to gather it all first, like you are = doing:Parser parser =3D new Parser ("http://www.yahoo.com");NodeList conten= t =3D parser.parse (null);then apply your visitor:content.visitAllNodesWith= (my_visitor);then make it into html again:String s =3D content.toHtml ();D= errick----- Original Message ----From: Srinivas Vippagunta <vippagunta@hotm= ail.com>To: htm...@li...Sent: Tuesday, June 26, 20= 07 8:25:47 PMSubject: [Htmlparser-user] how to add new nodes/elements to ht= ml source=0A= =0A= =0A= Hello All, I am pretty new to Java and was wondering if anyone can help= me out with my question. I am trying to parse some html source and based o= n some matching content I should add new nodes elements. For eg: I should b= e able to add a similar tr and td elements to the existing source but with = different td values. <tr id=3Dtest> <td id=3Dtest1><b>KEY</b></td> = <td id=3Dtest1><b>VALUE</b></td> </tr>I am doing something like thi= s: public class MyVisitor extends NodeVisitor { NodeList nl =3D new No= deList(); public MyVisitor () { =0A= } public void visitTag (Tag tag) { if ((tag instanceo= f TableRow)) { TagNode tc =3D new TableCo= lumn(); tc.setAttribute("id","testvalue",'"'); tc= .setAttribute("class","valid",'"'); nl.add(new TextNode("Test3= ")); tc.setChildren(nl); tc.setEndTag(new TableCo= lumn()); =0A= System.out.println(tc.toHtml()); = } } public static void main (String[] args) throws ParserExcep= tion { Parser parser =3D new Parser ("http://www.yahoo.com"); = MyVisitor visitor =3D new MyVisitor (); NodeList list =3D pa= rser.parse (null); list.visitAllNodesWith(visitor); } }This pri= nts: <TD id=3D"testvalue"=0A= class=3D"valid">Test3<TD> --> not a closing TD??? Also, how can I modif= y the current html source, the nodevisitor travels ? Can someone help!!Chee= rs,=0A= Srinivas.Make every IM count. Download Windows Live Messenger and join the = i=A2m Initiative now. It=A2s free. Make it count!-------------------------= ------------------------------------------------This SF.net email is sponso= red by DB2 ExpressDownload DB2 Express C - the FREE version of DB2 express = and takecontrol of your XML. No limits. Just data. Click to get it now.http= ://sourceforge.net/powerbar/db2/___________________________________________= ____Htmlparser-user mailing lis...@li...https:= //lists.sourceforge.net/lists/listinfo/htmlparser-user _________________________________________________________________ With Windows Live Hotmail, you can personalize your inbox with your favorit= e color. www.windowslive-hotmail.com/learnmore/personalize.html?locale=3Den-us&ocid= =3DTXT_TAGLM_HMWL_reten_addcolor_0607= |