3
votes

This problem has been driving me mad. I'm trying to use Java in ColdFusion to create a Document object. When I do this:

nd = createObject("java","javax.xml.parsers.DocumentBuilder");

I can dump nd and see that it correctly loaded all the methods:

object of javax.xml.parsers.DocumentBuilder Class Name javax.xml.parsers.DocumentBuilder 

Method / Return Type

getDOMImplementation() / org.w3c.dom.DOMImplementation

getSchema() / javax.xml.validation.Schema 

isNamespaceAware() / boolean 

isValidating() / boolean 

isXIncludeAware() / boolean 

newDocument() / org.w3c.dom.Document 

parse(java.io.File) / org.w3c.dom.Document 

parse(java.lang.String) / org.w3c.dom.Document 

parse(org.xml.sax.InputSource) / org.w3c.dom.Document 

parse(java.io.InputStream, java.lang.String) / org.w3c.dom.Document 

parse(java.io.InputStream) / org.w3c.dom.Document 

reset() / void 

setEntityResolver(org.xml.sax.EntityResolver) / void 

setErrorHandler(org.xml.sax.ErrorHandler) / void 

I'm trying to call the newDocument() method. I've tried all the following in both cfscript and cfsets:

nd.newDocument();
nd.newDocument(JavaCast("null",""));
nd = createObject("java","javax.xml.parsers.DocumentBuilder").newDocument();
nd = createObject("java","javax.xml.parsers.DocumentBuilder").newDocument(JavaCast("null",""));

But, no matter what approach I try, I get this error:

Either there are no methods with the specified method name and argument types or the isNamespaceAware 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.

I can see that the method was loaded.. The method isn't overloaded.. It doesn't require any arguments.. And, even when I explicitly tell CF I'm passing in null, it can't find the method..

I tried accessing the other methods in the class - and it couldn't find those either.. I'm not sure why I can dump the contents of the class - and I can see all the methods.. But, somehow CF is getting confused and can't find them when I try to call them..

Any ideas would be super helpful..

Thanks!!

2

2 Answers

7
votes

You have to create a an object for documentBuilder factory. With an help of factory you can get the realted xml informations. Here I've created the object and call the parse method by using documentbuilderfactory. Also you have to inject a newInstance() then only you can access the newdocument() methods. My Xml Content : testParse.xml

<?xml version="1.0"?>
<company>
<staff id="1001">
    <firstname>yong</firstname>
    <lastname>mook kim</lastname>
    <nickname>mkyong</nickname>
    <salary>100000</salary>
</staff>
<staff id="2001">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>200000</salary>
</staff>
</company>

*CF code :

<cfset myObj = createObject("java","javax.xml.parsers.DocumentBuilderFactory")>
<cfset createDocs = myObj.newInstance().newDocumentBuilder()>
<cfset parseDocs = createDocs.parse(expandpath('/testParse.xml'))>
<cfset getNodeName = parseDocs.getDocumentElement().getNodeName()>
<cfset getList = parseDocs.getElementsByTagName("staff")>

<cfloop index="i" from="1" to="#getList.getlength()#">
     <!--- Do your business logic here  --->
</cfloop>

I hope it will helps for you. Thanks.

0
votes

I got the same error calling methods from ColdFusion on a java class. I was trying to use the setPropertyName method as shown below.

    <cfobject action="create" type="java" name="This.txnRequest" class="#AnetAPI#.TransactionRequestType" />
    <cfset authTxnType=CreateObject("java", "#AnetAPI#.TransactionTypeEnum") />
    <cfset This.txnRequest.setTransactionType(authTxnType.AUTH_CAPTURE_TRANSACTION) />

Turns out that ColdFusion expects you to access the properties in EJBs directly. You also need to call the EJB constructor explicitly like this:

    <cfset This.txnRequest.init() />
    <cfset This.txnRequest.TransactionType=authTxnType.AUTH_CAPTURE_TRANSACTION />

CF implicitly calls the set method for you as described in the Adobe Docs. You cannot call it directly.