我想在Java中編寫水壺步驟,它採用輸入XML和XSLT文件並返回輸出轉換後的XML。Pentaho水壺使用xslt文件轉換xml java
我該怎麼做?我不知道從哪裏開始,並且沒有足夠的文檔給Java和Web上的Pentaho。
我想在Java中編寫水壺步驟,它採用輸入XML和XSLT文件並返回輸出轉換後的XML。Pentaho水壺使用xslt文件轉換xml java
我該怎麼做?我不知道從哪裏開始,並且沒有足夠的文檔給Java和Web上的Pentaho。
我找到了答案代碼在釜Java中使用XSLT轉換XML:
import java.util.*;
import java.io.FileOutputStream;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
private int infilenameIndex;
private int xsltfilenameIndex;
private int outfilenameIndex;
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
Object[] r=getRow();
if (r==null) {
setOutputDone();
return false;
}
if (first == false) {
infilenameIndex = getInputRowMeta().indexOfValue(getParameter("infilename"));
if (infilenameIndex < 0) {
throw new KettleException("Field not found in the input row, check parameter 'infilename'!");
}
xsltfilenameIndex = getInputRowMeta().indexOfValue(getParameter("xsltfilename"));
if (xsltfilenameIndex < 0) {
throw new KettleException("Field not found in the input row, check parameter 'xsltfilename'!");
}
outfilenameIndex = getInputRowMeta().indexOfValue(getParameter("outfilename"));
if (outfilenameIndex < 0) {
throw new KettleException("Field not found in the input row, check parameter 'outfilename'!");
}
first=false;
}
String infilename = get(Fields.In, "infilename").getString(r);
String xsltfilename = get(Fields.In, "xsltfilename").getString(r);
String outfilename = get(Fields.In, "outfilename").getString(r);
Object[] outputRowData = RowDataUtil.resizeArray(r, data.outputRowMeta.size());
int outputIndex = getInputRowMeta().size();
transform(infilename, xsltfilename, outfilename);
putRow(data.outputRowMeta, outputRowData);
return true;
}
public void transform(String infilename, String xsltfilename, String outfilename) throws KettleException {
javax.xml.transform.stream.StreamSource inss = null;
javax.xml.transform.stream.StreamSource xsltss = null;
javax.xml.transform.stream.StreamResult outss = null;
logBasic("");
logBasic("Transformerar " + infilename + " med " + xsltfilename + " till " + outfilename);
logBasic("");
try {
inss = new javax.xml.transform.stream.StreamSource(infilename);
}
catch (Exception e) {
logError("Infil saknas " + infilename);
throw new KettleException(e);
}
try {
xsltss = new javax.xml.transform.stream.StreamSource(xsltfilename);
}
catch (Exception e) {
logError("Xsltfil saknas " + xsltfilename);
throw new KettleException(e);
}
try {
outss = new javax.xml.transform.stream.StreamResult(outfilename);
}
catch (Exception e) {
logError("Outfil saknas " + outfilename);
throw new KettleException(e);
}
try {
TransformerFactory tFactory = TransformerFactory.newInstance();
// Set the TransformerFactory to the SAXON implementation.
//tFactory = new net.sf.saxon.TransformerFactoryImpl();
Transformer transformer = tFactory.newTransformer(xsltss);
// Do the transfromtation
transformer.transform(inss, outss);
}
catch (Exception e) {
throw new KettleException(e);
}
return;
}
如果你發現在網上你應該提供鏈接出於禮貌。但是我不明白爲什麼你不使用內置的「XSL轉換」步驟。 – Codek 2014-11-06 08:46:49
我可以看到這將是可取的大型XML文件,因爲它允許對XML的文件/基於流的處理而不是將整個XML保存在Pentaho變量中。由於大量生成的XML數據集,我最大化了我的JVM堆,所以我希望這個代碼 - 或者一個變體 - 可能會讓我不能將完整的XML保存在內存中......會報告回來!
這裏的示例代碼重構我/整合:
import java.util.*;
import java.io.FileOutputStream;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.*;
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
Object[] row = getRow();
if (row == null) {
setOutputDone();
return false;
}
String infilename = getStringField(row, "infilename");
String xsltfilename = getStringField(row, "xsltfilename");
String outfilename = getStringField(row, "outfilename");
Object[] outputRowData = RowDataUtil.resizeArray(row, data.outputRowMeta.size());
doXSLT(infilename, xsltfilename, outfilename);
putRow(data.outputRowMeta, outputRowData);
return true;
}
private String getStringField(Object[] row, String paramName) throws KettleException {
String value = get(Fields.In, paramName).getString(row);
if(value == null || "".equals(value)) {
throw new KettleException("XSLT setting '" + paramName + "' not found in input row");
} else {
return value;
}
}
public void doXSLT(String infilename, String xsltfilename, String outfilename) throws KettleException {
logBasic("");
logBasic("Transforming XML file '" + infilename + "' with XSLT '" + xsltfilename + "' and target file '" + outfilename + "'");
logBasic("");
try {
StreamSource inss = new StreamSource("file:///"+infilename);
StreamSource xsltss = new StreamSource("file:///"+xsltfilename);
StreamResult outss = new StreamResult("file:///"+outfilename);
TransformerFactory tFactory = TransformerFactory.newInstance();
// Set the TransformerFactory to the SAXON implementation.
//tFactory = new net.sf.saxon.TransformerFactoryImpl();
Transformer transformer = tFactory.newTransformer(xsltss);
transformer.transform(inss, outss);
} catch (Exception e) {
logError("Error attempting XSLT: " + e);
throw new KettleException(e);
}
return;
}
在最近的過去,你已經問了好幾個問題廣泛,你明確地指出你「不知道從哪裏開始」。如果是這樣,Stackoverflow不是你的問題的地方。如果您對Java,XSLT,XML,Pentaho和Kettle不瞭解,我建議您先閱讀本書或教程。 – 2014-11-03 13:47:19
建議您將標題修改爲「Pentaho Kettle使用XSLT轉換XML文件」,因爲重要的細微差別在於此技術針對XML文件工作,而內置的Pentaho XSLT支持僅適用於XML字段/變量。 – 2017-07-13 15:55:01