update text in HTML page using parser
I always gets error in the middlebitparent.replaceWith(nodespan);
in the following code which is written in jsoup to navigate the HTML doc and change the background color of word "In"
Elements divs= doc.select("div");
for(Element div : divs)
{
if (div.hasText())
{
int pos = div.text().indexOf(开发者_如何学编程"In");
out.println(pos);
if (pos >= 0)
{
Element span = doc.createElement("span");
span.attr("style", "background-color: yellow");
TextNode text = new TextNode(div.text(),"");
// String[] words = div.split("\\s", 4);
TextNode middlebit = text.splitText(4);
TextNode endbit=text.splitText("In".length());
Node middleclone = middlebit.clone();
span.appendChild(middleclone);
Node nodespan=span.clone();
Node middlebitparent = middlebit.parent();
middlebitparent.replaceWith(nodespan);
The error says
java.lang.NullPointerException
org.apache.jsp.Page1_jsp._jspService(Page1_jsp.java:177)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
UPDATE:
Is there any parser which can update text in text node in an easy way
public static void main(String[] arg){
HtmlCleaner cleaner = new HtmlCleaner();
try {
TagNode nodes = cleaner.clean(new File("c:/test.xml"));
Object[] objects = nodes.evaluateXPath("//div/a[text(.,'In')]");
System.out.println(((TagNode)objects[0]).getText());
} catch (Exception e) {
e.printStackTrace();
}
}
You can try HtmlCleaner which support XPath which will be more useful in your case. Jsoup's CSS selector cannot filter the text node.
精彩评论