【问题标题】:How to format XML using Pentaho如何使用 Pentaho 格式化 XML
【发布时间】:2021-02-05 06:10:36
【问题描述】:

我正在使用几个步骤生成 XML,最后,由于 XML 的复杂性(嵌套在嵌套内部嵌套内部),我不得不使用文本文件输出步骤并将“扩展”选项更改为'.xml'。

问题是我得到了一个包含格式良好的 XML 的单行 .xml 文件; 如果我将这一行复制粘贴到在线的 xmlFormatter 中,它会完美运行。

有没有什么方法可以将那一行文件作为字符串读取并将其更改为形状良好的 XML 文件?

获得:

假装:

提前致谢。

【问题讨论】:

  • 我没有真正的解决方案(我什至不推荐它),但您是否尝试过将文本输出的结果反馈回 XML 格式化程序?我也想知道这个问题的解决方案。

标签: xml pentaho pentaho-spoon pentaho-data-integration


【解决方案1】:

我建议使用 用户定义的 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 客户端测试的

【讨论】:

    猜你喜欢
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多