webMethods

webMethods

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
Expand all | Collapse all

java.lang.ClassCastException: class org.apache.xerces.dom.DeferredDocumentImpl cannot be cast to class com.wm.lang.xml.Document

  • 1.  java.lang.ClassCastException: class org.apache.xerces.dom.DeferredDocumentImpl cannot be cast to class com.wm.lang.xml.Document

    Posted Wed August 30, 2023 03:23 AM

    webMethods ver 10.15

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db;
    db = dbf.newDocumentBuilder();
    Document doc = (Document) db.parse( new FileInputStream(new File(“D:\flow.xml”)));

    error logs

    java.lang.ClassCastException: class org.apache.xerces.dom.DeferredDocumentImpl cannot be cast to class com.wm.lang.xml.Document (org.apache.xerces.dom.DeferredDocumentImpl and com.wm.lang.xml.Document are in unnamed module of loader java.net.URLClassLoader @63f8709c)


    #Integration-Server-and-ESB
    #webMethods
    #Flow-and-Java-services


  • 2.  RE: java.lang.ClassCastException: class org.apache.xerces.dom.DeferredDocumentImpl cannot be cast to class com.wm.lang.xml.Document

    Posted Wed August 30, 2023 08:59 AM

    Hi Sandeep,

    why do you want to read the file like this?

    Why not using the Build-In-Services from pub.file-Folder and pub.xml-Folder?
    I.e.:
    pub.file:getFile (as String)
    pub.xml:xmlStringToXMLNode
    pub.xml:xmlNodeToDocument

    Regards,
    Holger


    #Integration-Server-and-ESB
    #webMethods
    #Flow-and-Java-services


  • 3.  RE: java.lang.ClassCastException: class org.apache.xerces.dom.DeferredDocumentImpl cannot be cast to class com.wm.lang.xml.Document

    Posted Wed August 30, 2023 09:21 AM

    After reading/parsing xml file written some complex logic in java.


    #Flow-and-Java-services
    #Integration-Server-and-ESB
    #webMethods


  • 4.  RE: java.lang.ClassCastException: class org.apache.xerces.dom.DeferredDocumentImpl cannot be cast to class com.wm.lang.xml.Document

    Posted Wed August 30, 2023 10:36 AM

    Hi Sandeep,

    as the error message indicates, the classes used here are not meant for public use.
    This is related to the new class loading philosophy in Java.

    This was recently discussed here in the community in another thread where some wanted to deploy package which was working on IS 10.5 to IS 10.15 where it was not working any longer.
    Unfortunately I cannot find this thread currently.

    Regards,
    Holger


    #webMethods
    #Integration-Server-and-ESB
    #Flow-and-Java-services


  • 5.  RE: java.lang.ClassCastException: class org.apache.xerces.dom.DeferredDocumentImpl cannot be cast to class com.wm.lang.xml.Document

    Posted Wed August 30, 2023 11:51 AM

    Why are you casting to document then if all you need to do is parse the XML file in a Java service? Why not threat it as POJO and do everything in java service and push whatever you need back to the pipeline?

    I also recommend against making things over complicated. You can still get your document as Holger told and parse however you want to parse it using Java or flow services. I don’t see the benefit off parsing the file manually.


    #Integration-Server-and-ESB
    #Flow-and-Java-services
    #webMethods


  • 6.  RE: java.lang.ClassCastException: class org.apache.xerces.dom.DeferredDocumentImpl cannot be cast to class com.wm.lang.xml.Document

    Posted Thu August 31, 2023 03:22 PM

    Sandeep,

    Its not recommended to use that approach as you wont be able to use the created Document to visually manage any mappings. But if you are sure of your use case and really want this you have to make sure you use org.w3c.dom.Document and not com.wm.lang.xml.Document. You can do that by fixing you import statements in the code or specifically fixing the class name as such:

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db;
    db = dbf.newDocumentBuilder();
    org.w3c.dom.Document doc = db.parse( new FileInputStream(new File(“D:\flow.xml”)));
    

    Rupinder Singh


    #webMethods
    #Integration-Server-and-ESB
    #Flow-and-Java-services