我建议使用 用户定义的 Java 类 步骤并编写您自己的代码来将您的 XML 单行代码转换为印刷精美的版本。 Pentaho 已经为 XML 操作提供了各种库 JAR,您可以直接使用它们。
这是我编写的测试转换的样子:
生成 XML 字符串 在“xml”字段中写入包含 XML 单行字符串的单行。
格式化 XML 在其“处理器”选项卡下包含以下代码:
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathConstants;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;
import java.io.ByteArrayInputStream;
public static String toPrettyString(String xml) {
try {
// Turn xml string into a document
Document document = DocumentBuilderFactory.newInstance()
.newDocumentBuilder()
.parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
// Remove whitespaces outside tags
document.normalize();
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']",
document,
XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); ++i) {
Node node = nodeList.item(i);
node.getParentNode().removeChild(node);
}
// Setup pretty print options
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
// Return pretty print xml string
StringWriter stringWriter = new StringWriter();
transformer.transform(new DOMSource(document), new StreamResult(stringWriter));
return stringWriter.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
{
// First, get a row from the default input hop
Object[] r = getRow();
// If the row object is null, we are done processing.
if (r == null) {
setOutputDone();
return false;
}
// Init output row
Object[] outputRow = createOutputRow(r, data.outputRowMeta.size());
// Getting fields
String xml = get(Fields.In, "xml").getString(r);
// Init error handling
boolean rowInError = false;
String errMsg = "";
int errCnt = 0;
// Init Output
String xml_pretty = "";
// Put the xml in pretty format
try{
xml_pretty = toPrettyString(xml);
}
catch (Exception ex) {
errMsg = ex.getMessage();
errCnt++;
rowInError = true;
}
// Set the value in the output field
//
get(Fields.Out, "result").setValue(outputRow, true);
get(Fields.Out, "xml_pretty").setValue(outputRow, xml_pretty);
if ( !rowInError ) {
// putRow will send the row on to the default output hop.
//
putRow(data.outputRowMeta, outputRow);
}
else {
// putError will send the row on to the error hop.
//
get(Fields.Out, "result").setValue(outputRow, false);
get(Fields.Out, "xml_pretty").setValue(outputRow, "");
putError(data.outputRowMeta, outputRow, errCnt, errMsg, "", "ERR_0");
}
return true;
}
toPrettyString(String xml) 的实现由你决定。这里我使用了this SO answer 中的代码。您还必须在步骤的“字段”选项卡下定义输出字段(通过给出它们的名称和类型)。
以上代码是在 Pentaho 8.3.0.10 上使用 Spoon/PDI 客户端测试的