1

I am using JSoup 1.8.3 with ColdFusion 10 and have selected a DOM element. I am trying to call ownText(), which takes no arguments and I'm not giving it any, but I keep getting an error:

"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."

The error text suggests that the problem might be fixed by more clearly casting an argument, but there are no arguments. I tried adding one cast to null, but this did not fix the problem.

The same error happens if I try to call textNodes(). However, I can call text() and it returns exactly what one would expect from the call (a superset of what I'm looking for). 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 as it should be, and I can see both ownText() and textNodes() right there in the list of methods that should be available, alongside text() that works fine.

Why are some of these methods not working, and how might I access them?

Code example:

<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>

And the output looks like this, highlighting that the desired methods are listed:

1
  • 2
    Could you provide the example in question ? With the DOM element you're trying to parse ? Aug 20, 2015 at 20:45

1 Answer 1

3

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

Not quite.

textSection is an array that contains instances of that class. However, the variable itself is actually an instance of org.jsoup.select.Elements (note the "s", plural, totally different class). This other class just so just happens to contain a method named text() as well, but none named ownText(). Hence the exception, ie "... there are no methods with the specified method name ...".

You need to grab one of the objects inside the array, ie an Element (singular) and invoke the method on that object instead, ie:

textSection[1].ownText(); 

NB: Technically speaking, the org.jsoup.select.Elements is not an array per se, but a class that implements java.util.List. So it can be used like a CF array (in most respects).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.