One Star

XML mapping with REST client

The requirement I need is to be able to monitor a folder directory for a formatted .cvs file and when that gets triggered, I take that read the file and map the data into a UTF-8 XML file to pass to a REST client web service call (not my REST service). The problem is, the REST service requires that the passed XML file has a new line after each tag (I think people call that pretty XML) and the XML mapping does not provide that feature so I was wondering what my next approach should be or if it's just me doing something wrong. (Also note that I'm new to Talend).
Everything is mapping fine data-wise but the required format is not what I need and I'm pretty sure my tRESTClient is configured correctly as the web service is getting called.
I've attached a screen of the process (hopefully you can see it). My tRESTClient is a POST and the content type and accepted type are both XML. I also unticked the "convert response to DOM document" in the advanced setting but that shouldn't affect the input structure.
11 REPLIES
One Star

Re: XML mapping with REST client

I've posted this question a while ago now but there hasn't been any replies to either help or request for more information.
Does that mean there's no chance of this working, or am I just not getting something right and no one is bothering with this question?
Can someone please respond with any hints or ideas that I can use because I'm all out of ideas and nothing is working?
Community Manager

Re: XML mapping with REST client

Hi 

 The problem is, the REST service requires that the passed XML file has a new line after each tag (I think people call that pretty XML)

What are your expected format? Can you show us an example?
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: XML mapping with REST client

The actual required XML file will be too long and not necessary but an example of the XML format I need would be as below:
<task>
<job>JOB_NAME</job>
</tasks>

I just need it so that the XML passed to the REST client has a new line after each ending tag.
Community Manager

Re: XML mapping with REST client

Hi 
Take a look at this example, it shows how to read a document and print  a pretty XML string. I am not sure if there exists API used to generate a pretty document. If exists, you can create a routine and call the routine to transform it after the document is generated with tXMLMap.
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: XML mapping with REST client

I am not sure if there exists API used to generate a pretty document. 

So from what I understand from this statement, does that mean I need to use the Java code from you example and put it into a tJava component because there isn't a pre-defined component by Talend to make pretty XML?
If exists, you can create a routine and call the routine to transform it after the document is generated with tXMLMap.

If that's the case, how do I use the Java code to retrieve the XML string from the tXMLMap? What is the global variable I need to use or if I just connect the tXMLMap to the tJava component, what do I need to do in code to be get the string from tXMLMap so that I can convert it to be pretty XML. Also how do I then send the pretty XML over to the tRESTClient? If I just do a return and connect the tJava component to the tRESTClient, will that work?
Sorry, but I'm new to Talend so I might need a little more guidance. The examples in all your documents show only the tJava component printing out data. The example link you gave also only logs the pretty XML, it had nothing to do with how to send it to the next component.
Community Manager

Re: XML mapping with REST client

Hi
Read this guide to know how to create a routine and call it in a job, tXMLMap generates a document, send this document as input parameter to the routine function, and the function should return a document for used on tRestCient, you can call the routine on tJavaRow or tMap, for example:
...tXMLMap--main--tJavaRow(or tMap)--main--tRestClient...
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: XML mapping with REST client

Hi,
I tried implementing everything as explained but now I'm getting the error:
The method parse(Document) in type PrettyXMLParsing is not applicable for the arguments (Document)

Why does this error occur and how do I fix it?
I added a tJavaRow in between my tXMLMap and tRESTClient where the code I added for the tJavaRows is:
 output_row.body = new PrettyXMLParsing().parse(input_row.body);

Where body in both the input and output row is a Document type.
Also for my routine, it's called PrettyXMLParsing as seen from the tJavaRow code snippet with the code as below:
package routines;
import java.io.FileInputStream;
import java.io.StringWriter;
import java.io.Writer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
public class PrettyXMLParsing {
public Document parse(Document xml) throws Exception {
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
Writer out = new StringWriter();
tf.transform(new DOMSource(xml), new StreamResult(out));
System.out.print( out.toString());
return convertStringToDoc(out.toString());
}//parse
private Document convertStringToDoc(String fileContentStr){
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();

Document outputDoc = db.parse(new FileInputStream(fileContentStr));
return outputDoc;
}catch(Exception e){
e.printStackTrace();
}
return null;
}//convertStringToDoc
}//PrettyXMLParsing

I didn't change much of any of the sample you gave me and just took it directly how it was. I just tweaked it a little so that it fits my requirement.
Did I do something wrong somewhere?
UPDATE:
I think it's due to the difference between data type of Document passed from the tJavaRow to the routine. I tried just putting the routine code directly in the tJavaRow and the error says that it cannot convert between org.w3c.dom.Document to routines.system.Document so I'm guessing the conversion is correct but the Document I return from my PrettyXMLParsing().parse(Document) is not the correct object type. But that still doesn't help me make it work.
The Document type I'm passing back from the routing is org.w3c.dom.Document but the type needed by the output_row.body is routines.system.Document. How do I convert between the two type of Document type now?
Community Manager

Re: XML mapping with REST client

Hi
 output_row.body = new PrettyXMLParsing().parse(input_row.body);

You don't call the routine correctly, the right expression is:
output_row.body=PrettyXMLParsing.parse(input_row.body);

Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: XML mapping with REST client

I tried that as well, it's still the same error for me. Could it be the issue I described in my update where the Document type is not the same?
One Star

Re: XML mapping with REST client

I've managed to use the routine code to convert the XML map document to be a pretty document by converting the input row of type routines.system.Document to a string, then using the pretty XML example previously provided to make a pretty XML document of type org.w3c.dom.Document.
The problem I'm having now is the document type of output_row.body is routines.system.Document but the pretty XML document is org.w3c.dom.Document.
Can someone help explain to me how I can convert from org.w3c.dom.Document to routines.system.Document?
Community Manager

Re: XML mapping with REST client

Hi
I will take a look at your code and come back to you.
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business