Six Stars

Type mismatch : can not convert from org.w3c.dom.Document to routines.system.Document

Recently we have upgraded a talend job from TOS 5.3.2 to 6.1.0 but started getting a compile time error "Type mismatch : can not convert from org.w3c.dom.Document to routines.system.Document". This is happening while saving a data from tESBProviderRequest into xml file.

 

PS : This was working fine in 5.3.2 so I think there should be no concern of job design. Regarding the fix, I am sure it can be done from manual conversions. But what I want to know is, Is it the version problem?

3 REPLIES
Fifteen Stars

Re: Type mismatch : can not convert from org.w3c.dom.Document to routines.system.Document

This is an annoying "feature" when working with XML. However it usually is experienced if you use your own Java code to work with XML. An easy way to get round this is to convert the XML (of class type  org.w3c.dom.Document) to a String, remove the XML signature and then use the tConvertType component to convert from String to Talend' Document class.

 

It's a pain, but it works. I'm not sure why Talend have their own Document class for XML, but I have built a routine with Java to handle this scenario. 

 

Here is some code.....

To remove the XML header from the String....

    public static String removeXMLHeader(String xml){
    	if(xml!=null){
    		xml = xml.replaceAll("\\<\\?xml(.+?)\\?\\>", "").trim();
    	}
    	
    	return xml;
    }

To convert from an org.w3c.Document to String....

    public static String getStringFromDocument(Document doc)
    {
        try
        {
           DOMSource domSource = new DOMSource(doc);
           StringWriter writer = new StringWriter();
           StreamResult result = new StreamResult(writer);
           TransformerFactory tf = TransformerFactory.newInstance();
           Transformer transformer = tf.newTransformer();
           transformer.transform(domSource, result);
           return writer.toString();
        }
        catch(TransformerException ex)
        {
           ex.printStackTrace();
           return null;
        }
    } 
Rilhia Solutions
Six Stars

Re: Type mismatch : can not convert from org.w3c.dom.Document to routines.system.Document

Hi Richard,

thanks for the reply. 

 

Eventually i got it worked with parsing. ParserUtils.parseTo_Document

However my question is still the same. It was working fine in 5.3.2 (there i did not need any conversion), why it caused problem when the job is upgraded to 6.1.0 ?

Fifteen Stars

Re: Type mismatch : can not convert from org.w3c.dom.Document to routines.system.Document

I don't believe Talend introduced a change to how it interacts with XML between those versions. I certainly don't remember having to significantly change anything I worked on. The only thing I can think of is maybe the change of Java version. It would be very hard to diagnose precisely why you experienced this without having your code and seeing this for myself. 

Rilhia Solutions