ColdFusion Java method exception not found
This question makes me very angry I'm trying to use java to create document objects in ColdFusion When I do this:
nd = createObject("java","javax.xml.parsers.DocumentBuilder");
I can dump nd and see that it loads all the methods correctly:
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 tried all of 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",""));
However, no matter what method I try, I will receive this error:
I can see the method loaded Method is not overloaded It does not require any parameters Moreover, even if I explicitly tell CF that I pass in null, it can't find a method
I try to access other methods in the class - it can't find those I don't know why I can dump the contents of the class - I can see all the methods But somehow CF was getting it. When I tried to call them, they were confused and couldn't find them
Any idea will be super useful
thank you!!
Solution
You must create an object for the documentbuilder factory With the help of the factory, you can get the validated XML information Here, I create the object and call the parse method using documentbuilderfactory
<?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 help you thank you.