为什么 JSoup 中可能缺少某些方法?
Why might some methods be missing in JSoup?
我将 JSoup 1.8.3 与 ColdFusion 10 一起使用,并选择了 DOM element. I am trying to call ownText(),它不带任何参数,我也没有给它任何参数,但我一直收到错误消息:
"The ownText method was not found. Either there are no methods with
the specified method name and argument types or the ownText method is
overloaded with argument types that ColdFusion cannot decipher
reliably. ColdFusion found 0 methods that match the provided
arguments. If this is a Java object and you verified that the method
exists, use the javacast function to reduce ambiguity."
错误文本表明可以通过更明确地提出论点来解决问题,但没有论据。我尝试向 null 添加一个转换,但这并没有解决问题。
如果我尝试调用 textNodes(),也会发生同样的错误。但是,我可以调用 text() 并且它 returns 正是人们对调用的期望(我正在寻找的超集)。当我转储我正在调用的变量时,在调用之前,我看到它应该是 class org.jsoup.nodes.Element,我可以看到 ownText() 和 textNodes( ) 就在应该可用的方法列表中,旁边是可以正常工作的 text() 。
为什么其中一些方法不起作用,我该如何访问它们?
代码示例:
<CFOUTPUT>
<cfset html = ' <html> ' >
<cfset html &= ' <head> <title> Bug Demo</title> </head> ' >
<cfset html &= ' <body><div class="wrapper" > ' >
<cfset html &= ' <div class="textSection" > ' >
<cfset html &= ' <h3><a href="http://example.com"> Undesired Link Text </a></h3> ' >
<cfset html &= ' This is the target text that the code below should extract.' >
<cfset html &= ' </div> ' >
<cfset html &= ' <div class="imageSection" > ' >
<cfset html &= ' <a href="http://example.com"><img src="/example.png"/></a> ' >
<cfset html &= ' </div> ' >
<cfset html &= ' </div> ' >
<cfset html &= '</body></html> '>
<cfscript>
//Load jSoup
paths = arrayNew(1);
paths[1] = expandPath("jsoup-1.8.3.jar");
loader = createObject("component", "colab.javaloader.javaloader.JavaLoader").init(paths);
jSoupClass = loader.create("org.jsoup.Jsoup");
//Parse document
dom = jSoupClass.parse(html);
wrapper = dom.select( JavaCast("string", "div.wrapper") ).first();
textSection = wrapper.select("div.textSection");
writeDump(textSection); //show type and methods of textSection
targetText = textSection.ownText(); //Error: method not found - ???
//targetText = textSection.ownText(JavaCast("null", "")); //also not found, but there should be no parameter
//textNodes = textSection.textNodes(); //Also not found
tooMuchInfo = textSection.text(); //works just fine
WriteOutput(tooMuchInfo);//produces "Undesired Link Text This is the target text that the code below should extract. "
</cfscript>
</CFOUTPUT>
输出如下所示,突出显示列出了所需的方法:
When I dump the variable I'm making the call on, immediately before the call, I see that it's of the class org.jsoup.nodes.Element
不完全是。
textSection
是一个数组,其中 包含 个 class 的实例。但是,变量本身实际上也是 org.jsoup.select.Elements
(note the "s", plural, totally different class). This other class just so just happens to contain a method named text() 的一个实例,只是 none 命名为 ownText()。因此出现异常,即“...没有具有指定方法名称的方法...”。
您需要获取数组 内部 中的对象之一,即 Element
(单数)并调用该对象的方法,即:
textSection[1].ownText();
注意: 从技术上讲,org.jsoup.select.Elements
本身并不是数组,而是实现了 java.util.List
的 class。所以可以用like a CF array(在大多数方面)。
我将 JSoup 1.8.3 与 ColdFusion 10 一起使用,并选择了 DOM element. I am trying to call ownText(),它不带任何参数,我也没有给它任何参数,但我一直收到错误消息:
"The ownText method was not found. Either there are no methods with the specified method name and argument types or the ownText method is overloaded with argument types that ColdFusion cannot decipher reliably. ColdFusion found 0 methods that match the provided arguments. If this is a Java object and you verified that the method exists, use the javacast function to reduce ambiguity."
错误文本表明可以通过更明确地提出论点来解决问题,但没有论据。我尝试向 null 添加一个转换,但这并没有解决问题。
如果我尝试调用 textNodes(),也会发生同样的错误。但是,我可以调用 text() 并且它 returns 正是人们对调用的期望(我正在寻找的超集)。当我转储我正在调用的变量时,在调用之前,我看到它应该是 class org.jsoup.nodes.Element,我可以看到 ownText() 和 textNodes( ) 就在应该可用的方法列表中,旁边是可以正常工作的 text() 。
为什么其中一些方法不起作用,我该如何访问它们?
代码示例:
<CFOUTPUT>
<cfset html = ' <html> ' >
<cfset html &= ' <head> <title> Bug Demo</title> </head> ' >
<cfset html &= ' <body><div class="wrapper" > ' >
<cfset html &= ' <div class="textSection" > ' >
<cfset html &= ' <h3><a href="http://example.com"> Undesired Link Text </a></h3> ' >
<cfset html &= ' This is the target text that the code below should extract.' >
<cfset html &= ' </div> ' >
<cfset html &= ' <div class="imageSection" > ' >
<cfset html &= ' <a href="http://example.com"><img src="/example.png"/></a> ' >
<cfset html &= ' </div> ' >
<cfset html &= ' </div> ' >
<cfset html &= '</body></html> '>
<cfscript>
//Load jSoup
paths = arrayNew(1);
paths[1] = expandPath("jsoup-1.8.3.jar");
loader = createObject("component", "colab.javaloader.javaloader.JavaLoader").init(paths);
jSoupClass = loader.create("org.jsoup.Jsoup");
//Parse document
dom = jSoupClass.parse(html);
wrapper = dom.select( JavaCast("string", "div.wrapper") ).first();
textSection = wrapper.select("div.textSection");
writeDump(textSection); //show type and methods of textSection
targetText = textSection.ownText(); //Error: method not found - ???
//targetText = textSection.ownText(JavaCast("null", "")); //also not found, but there should be no parameter
//textNodes = textSection.textNodes(); //Also not found
tooMuchInfo = textSection.text(); //works just fine
WriteOutput(tooMuchInfo);//produces "Undesired Link Text This is the target text that the code below should extract. "
</cfscript>
</CFOUTPUT>
输出如下所示,突出显示列出了所需的方法:
When I dump the variable I'm making the call on, immediately before the call, I see that it's of the class org.jsoup.nodes.Element
不完全是。
textSection
是一个数组,其中 包含 个 class 的实例。但是,变量本身实际上也是 org.jsoup.select.Elements
(note the "s", plural, totally different class). This other class just so just happens to contain a method named text() 的一个实例,只是 none 命名为 ownText()。因此出现异常,即“...没有具有指定方法名称的方法...”。
您需要获取数组 内部 中的对象之一,即 Element
(单数)并调用该对象的方法,即:
textSection[1].ownText();
注意: 从技术上讲,org.jsoup.select.Elements
本身并不是数组,而是实现了 java.util.List
的 class。所以可以用like a CF array(在大多数方面)。