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 ()
{
}
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 .
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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 !
}
}
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 .