我有一个HTML文档,我需要更新IMG标签的text和src属性。我在Java工作。我想替换HTML中的以下字符串:DataName,DataText和DataIcon。替换Java中IMG标签的src属性
DataName
虽然我suceeded替换字符串数据名称和DataText,我还没有成功通过存储在数据库中的字符串我IMAGEURL更换DataIcon。检查调试说,它只是简单地不能搜索DataIcon字符串。我使用的HTMLParser,我已经写了下面的类应用问题:
public class MyNodeVisitor extends NodeVisitor {
String name;
String text;
String icon;
public MyNodeVisitor() {
}
public MyNodeVisitor(String IconPath, String Name, String Text){
this.name = Name;
this.text = Text;
this.icon = IconPath;
}
public void visitStringNode (Text string)
{
if (string.getText().equals("DataName")) {
string.setText(name);
}
else if(string.getText().equals("DataIcon")){
string.setText(icon);
}
else if (string.getText().equals("DataText")){
string.setText(text);
}
}
}
类已经在我的应用程序代码以这样的方式
NodeList nl = new NodeList();
String htmlString = null;
InputStream contentStream = null;
String textString = null;
String resultStr = getDatabaseAttribute(name,"DESCRIPTION");
String resultStr2 = getDatabaseAttribute(name,"NAME");
String resultStr3 = getDatabaseAttribute(name,"ICON_path");
try
{
// Read the URL content into a String using the default encoding (UTF-8).
contentStream = WWIO.openFileOrResourceStream(BROWSER_BALLOON, this.getClass());
htmlString = WWIO.readStreamToString(contentStream, null);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
WWIO.closeStream(contentStream, resultStr);
}
try {
Parser parser = new Parser(htmlString);
nl = parser.parse(null);
nl.visitAllNodesWith(new MyNodeVisitor(resultStr3, resultStr2,resultStr));
nl.toString();
} catch (ParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String output = nl.toHtml();
return output;
任何人可以帮助我得到了应用?整个问题是它无法在IMG标签中搜索DataIcon字符串。谢谢你的帮助。
2013-03-28
MichalB