我现在正在接受培训。我在mule studio中有以下代码,这只是java端。我认为这就是问题所在。
就像说的那样,csv文件来自它所在的文件夹,但它没有到达输出文件夹。欢迎任何建议或提示(如果已经工作了将近15个小时。)
package com.csvToXml.common;
import net.sf.flatpack.DefaultDataSet;
import net.sf.flatpack.DefaultParserFactory;
import net.sf.flatpack.Parser;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.BufferedReader;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class CsvConverter {
public static int numberOfRows;
public static int rowNumber = 0;
public static int columnNumber = 0;
private static char delimiter = ',';
private static char qualifier = '"';
private static boolean ignoreFirstRecord = true;
public CsvConverter() {
}
public String convert(String csv) {
StringReader sr = new StringReader(csv);
FileReader fr = null;
//Get the configuration file for csv processing
try {
fr = new FileReader("/users/jacqueslecoq/MuleStudio/workspace/stock/flows/usermap.xml");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//The csv parser
Parser p = DefaultParserFactory.getInstance().newDelimitedParser(fr, sr,delimiter, qualifier, ignoreFirstRecord);
DefaultDataSet d = (DefaultDataSet) p.parse();
//Build the xml
DocumentBuilderFactory dFact = DocumentBuilderFactory.newInstance();
DocumentBuilder build;
Document doc=null;
try {
build = dFact.newDocumentBuilder();
doc = build.newDocument();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Element root = doc.createElement("root");
doc.appendChild(root);
Element Details = doc.createElement("rows");
root.appendChild(Details);
while (d.next()) {
String[] colums = d.getColumns();
for(int i=0; i<colums.length;i++ ){
Element name = doc.createElement(colums[i]);
name.appendChild(doc.createTextNode(d.getString(colums[i])));
Details.appendChild(name);
}
}
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer=null;
StringWriter writer = null;
try {
transformer = tf.newTransformer();
writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
String output = writer.getBuffer().toString();
return output;
}
答案 0 :(得分:0)
因为您没有提供足够的信息来帮助您,所以我会给您随机的一般建议:)
因为你在骡子,有些事情你应该让骡子做而不是自己编码。例如,应该删除执行DOM到XML转换的方法的所有末尾,并替换为在组件之后添加的<mulexml:dom-to-xml-transformer />
。
另外,我想象usermap.xml
,因为它在你的项目中。如果是这种情况,为什么每次加载?您可以在CsvConverter()
中预加载一次。并且不要像这样硬编码文件路径,在Mule中部署应用程序时永远不会工作。相反,将usermap.xml
放入src/main/resources
并将其作为类路径资源加载Thread.currentThread().getContextClassloader().getResourceAsStream("usermap.xml")
PS。什么'顶部的所有静态变量?如果他们是不变的,那么也要做最后的决定。并为常量命名THE_JAVA_WAY
。