我正在尝试从TagNode
解析html。问题是,有一个内部异常阻止它工作。这是我的电话:
CleanerProperties props = new CleanerProperties();
SimpleHtmlSerializer serializer = new SimpleHtmlSerializer(props);
changes.setHtmlForTimetable(serializer.getAsString(root));
根当然不是空的,显然序列化程序也不是空的。
这是堆栈跟踪:
01-21 23:42:50.860: W/System.err(25196): java.lang.NullPointerException
01-21 23:42:50.865: W/System.err(25196): at org.htmlcleaner.HtmlSerializer.isMinimizedTagSyntax(HtmlSerializer.java:54)
01-21 23:42:50.875: W/System.err(25196): at org.htmlcleaner.HtmlSerializer.serializeOpenTag(HtmlSerializer.java:189)
01-21 23:42:50.880: W/System.err(25196): at org.htmlcleaner.SimpleHtmlSerializer.serialize(SimpleHtmlSerializer.java:52)
01-21 23:42:50.885: W/System.err(25196): at org.htmlcleaner.Serializer.write(Serializer.java:249)
01-21 23:42:50.890: W/System.err(25196): at org.htmlcleaner.Serializer.getAsString(Serializer.java:176)
01-21 23:42:50.900: W/System.err(25196): at org.htmlcleaner.Serializer.getAsString(Serializer.java:197)
01-21 23:42:50.905: W/System.err(25196): at org.htmlcleaner.Serializer.getAsString(Serializer.java:206)
01-21 23:42:50.915: W/System.err(25196): at com.roneven.blich.GetChanges$1$1$1.callback(GetChanges.java:125)
01-21 23:42:50.920: W/System.err(25196): at com.roneven.blich.GetChanges$1$1$1.callback(GetChanges.java:1)
01-21 23:42:50.930: W/System.err(25196): at com.androidquery.callback.AbstractAjaxCallback.callback(AbstractAjaxCallback.java:501)
01-21 23:42:50.935: W/System.err(25196): at com.androidquery.callback.AbstractAjaxCallback.afterWork(AbstractAjaxCallback.java:1269)
01-21 23:42:50.940: W/System.err(25196): at com.androidquery.callback.AbstractAjaxCallback.run(AbstractAjaxCallback.java:993)
01-21 23:42:50.945: W/System.err(25196): at android.os.Handler.handleCallback(Handler.java:725)
01-21 23:42:50.945: W/System.err(25196): at android.os.Handler.dispatchMessage(Handler.java:92)
01-21 23:42:50.950: W/System.err(25196): at android.os.Looper.loop(Looper.java:137)
01-21 23:42:50.950: W/System.err(25196): at android.app.ActivityThread.main(ActivityThread.java:5191)
01-21 23:42:50.955: W/System.err(25196): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 23:42:50.955: W/System.err(25196): at java.lang.reflect.Method.invoke(Method.java:511)
01-21 23:42:50.960: W/System.err(25196): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
01-21 23:42:50.960: W/System.err(25196): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
01-21 23:42:50.965: W/System.err(25196): at dalvik.system.NativeStart.main(Native Method)
更新:我找到了一个解决方法,甚至没有使用 HtmlSerializer 或 TagNode(使用 html 我已经成功提取和一些字符串命令)
根据HtmlCleaner的消息来源,HtmlSerializer.isMinimizedTagSyntax()
调用CleanerProperties.getTagInfoProvider()
。而在CleanerProperties.java
ITagInfoProvider tagInfoProvider = null;
...
public ITagInfoProvider getTagInfoProvider() {
return tagInfoProvider;
}
没有方法可以在CleanerProperties
中设置它。 在使用之前,必须以某种方式设置tagInfoProvider
。
我认为,要了解如何操作,您必须深入研究HtmlCleaner的文档,例如Java代码使用。
我遇到了同样的问题,并花了很多时间寻找解决方案。
CleanerProperties props = new CleanerProperties() {
@Override
public ITagInfoProvider getTagInfoProvider() {
return DefaultTagProvider.getInstance();
}
};
HtmlSerializer simpleHtmlSerializer = new SimpleHtmlSerializer(props);
String message = simpleHtmlSerializer.getAsString(tagNode, true);