使用 Pentaho 获取表中所有 td 的 XPath

发布于 2024-11-01 05:15:45 字数 779 浏览 1 评论 0原文

无论如何,是否使用 Pentaho 从 html 页面解析表 td? 假设我有这个 html 内容

<html>
  <body>
    <table>
      <tr>
        <td>info1</td>
        <td>info2</td>
      </tr>
      <tr>
        <td>info3</td>
        <td>info4</td>
      </tr>
    </table>
  </body>
</html>
I am using in Pentaho the "Get data from XML" with the following data:
Content::
Loop XPath: /html/body/table/tr
Fields::
Name: tableData
XPath: td
The data information I would like to have is
info1 info2 info3 info4
in any kind of way.
Any help would be truly appreciated!

Is there anyway using Pentaho to parse a tables td's from an html page?
Lets say I have this html content

<html>
  <body>
    <table>
      <tr>
        <td>info1</td>
        <td>info2</td>
      </tr>
      <tr>
        <td>info3</td>
        <td>info4</td>
      </tr>
    </table>
  </body>
</html>

I am using in Pentaho the "Get data from XML" with the following data:

Content::
Loop XPath: /html/body/table/tr
Fields::
Name: tableData
XPath: td


The data information I would like to have is

info1 info2 info3 info4

in any kind of way.
Any help would be truly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烂人 2024-11-08 05:15:45

我通过将文件的每一行作为行读取来解决这个问题。然后我添加了一个 Pentaho 步骤“用户定义的 Java 类”,并让它使用 XSLT 将我的表内容解析为一个新的 XML 文件。使用该 XML,我能够获取完成任务所需的数据。
这是我在“用户定义的 Java 类”中编写的内容:


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;
}

I solved it by making reading every row of my file as rows. Then I added a Pentaho step "User Defined Java Class" and made it parse my table content using XSLT to a new XML file. Using that XML I was able to get the data needed to complete the task.
Here is what I wrote in "User Defined Java Class":


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;
}

花心好男孩 2024-11-08 05:15:45

看到了这个。对于现在来这里的任何人。可以使用 jsoup 使用适当的路径来解析为 xml。它是一个简单的插件,可以在用户定义的类中与您拥有的任何其他方法一起使用。它是一个 CSS 选择器。

saw this. for anyone who is coming here now. parsing to xml can be done with jsoup using the appropriate path. its a simple plugin and works in the user defined class with whatever other methods you have. it is a css selector.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文