Talend:设置与列对应的属性名称和值

时间:2022-11-27 23:40:19

I have a csv as:

我有一个csv:

ID,Name,Age
1,qwerty,12
2,asdf,11
3,zxcvb,10

I need to create an xml as follows:

我需要创建一个xml,如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<entities>
    <entity>
        <field name="ID" value="1"/>
        <field name="Name" value="qwerty"/>
        <field name="Age" value="12"/>
    </entity>
        <entity>
        <field name="ID" value="2"/>
        <field name="Name" value="asdf"/>
        <field name="Age" value="11"/>
    </entity>
         <entity>
        <field name="ID" value="3"/>
        <field name="Name" value="zxcvb"/>
        <field name="Age" value="10"/>
    </entity>
</entities>

The column names vary from one csv file to another. So I want to make a generic job that takes in all the columns and generate an xml as shown. Any help would be appreciated

列名称从一个csv文件到另一个不同。所以我想创建一个通用作业,它接受所有列并生成一个xml,如图所示。任何帮助,将不胜感激

3 个解决方案

#1


1  

Since you asked for Talend -- it is easy if you know the column names beforehand, because then you only need tFileInputDelimited -> tFileOutputXML. If you don't know the column names (they are in the first row of your data file), then you need to use Talend Integration Suite (not Talend Open Studio), which supports "dynamic columns".

既然你要求Talend - 如果事先知道列名就很容易,因为那时你只需要tFileInputDelimited - > tFileOutputXML。如果您不知道列名称(它们位于数据文件的第一行),那么您需要使用支持“动态列”的Talend Integration Suite(而不是Talend Open Studio)。

Define the schema of your file as a single column of type Dynamic. That column will then contain all your data. But for writing it out as XML, you would need to do some Java coding by hand (in a tJavaFlex component). It's not difficult, and I could probably show you how to do it if you need further help.

将文件架构定义为Dynamic类型的单个列。然后该列将包含您的所有数据。但是要将其写成XML,您需要手动进行一些Java编码(在tJavaFlex组件中)。这并不困难,如果你需要进一步的帮助,我可能会告诉你如何做到这一点。

But as I said, this doesn't work in the Talend Open Studio version, only in the subscription version.

但正如我所说,这在Talend Open Studio版本中不起作用,仅在订阅版本中。

#2


0  

try this

尝试这个

   /**
   * Converts a CSV file to a simple XML file
 *
 * @param string $file
 * @param string $container
 * @param string $rows
 * @return string
 */
   function csv2xml($file, $container = 'data', $rows = 'row')
{
        $r = "<{$container}>\n";
        $row = 0;
        $cols = 0;
        $titles = array();

        $handle = @fopen($file, 'r');
        if (!$handle) return $handle;

        while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
        {
             if ($row > 0) $r .= "\t<{$rows}>\n";
             if (!$cols) $cols = count($data);
             for ($i = 0; $i < $cols; $i++)
             {
                  if ($row == 0)
                  {
                       $titles[$i] = $data[$i];
                       continue;
                  }

                  $r .= "\t\t<{$titles[$i]}>";
                  $r .= $data[$i];
                  $r .= "</{$titles[$i]}>\n";
             }
             if ($row > 0) $r .= "\t</{$rows}>\n";
             $row++;
        }
        fclose($handle);
        $r .= "</{$container}>";

        return $r;
}

$xml = csv2xml('/home/user/myfile.csv', 'people', 'person');

?>

found this at: http://www.hotscripts.com/listing/convert-a-csv-file-to-xml-using-php/

发现于:http://www.hotscripts.com/listing/convert-a-csv-file-to-xml-using-php/

#3


0  

Here is an XQuery program that should do it:

这是一个应该这样做的XQuery程序:

<entities>
{
  let $rows :=
    let $input := "ID,Name,Age
      1,qwerty,12
      2,asdf,11
      3,zxcvb,10"
    return tokenize($input, "&#10;")
  let $columns := tokenize(head($rows), ",")
  for $row in tail($rows)
  let $data := tokenize($row, ",")
  return 
    <entity>
    {
      for $column at $p in $columns
      return
        <field name="{$column}" value="{$data[$p]}"/>
    }
   </entity>
}
</entities>

Tested on try.zorba-xquery.com, it outputs:

在try.zorba-xquery.com上测试,它输出:

<?xml version="1.0" encoding="UTF-8"?>
<entities>
  <entity>
    <field name="ID" value=" 1"/>
    <field name="Name" value="qwerty"/>
    <field name="Age" value="12"/>
  </entity>
  <entity>
    <field name="ID" value=" 2"/>
    <field name="Name" value="asdf"/>
    <field name="Age" value="11"/>
  </entity>
  <entity>
    <field name="ID" value=" 3"/>
    <field name="Name" value="zxcvb"/>
    <field name="Age" value="10"/>
  </entity>
</entities>

#1


1  

Since you asked for Talend -- it is easy if you know the column names beforehand, because then you only need tFileInputDelimited -> tFileOutputXML. If you don't know the column names (they are in the first row of your data file), then you need to use Talend Integration Suite (not Talend Open Studio), which supports "dynamic columns".

既然你要求Talend - 如果事先知道列名就很容易,因为那时你只需要tFileInputDelimited - > tFileOutputXML。如果您不知道列名称(它们位于数据文件的第一行),那么您需要使用支持“动态列”的Talend Integration Suite(而不是Talend Open Studio)。

Define the schema of your file as a single column of type Dynamic. That column will then contain all your data. But for writing it out as XML, you would need to do some Java coding by hand (in a tJavaFlex component). It's not difficult, and I could probably show you how to do it if you need further help.

将文件架构定义为Dynamic类型的单个列。然后该列将包含您的所有数据。但是要将其写成XML,您需要手动进行一些Java编码(在tJavaFlex组件中)。这并不困难,如果你需要进一步的帮助,我可能会告诉你如何做到这一点。

But as I said, this doesn't work in the Talend Open Studio version, only in the subscription version.

但正如我所说,这在Talend Open Studio版本中不起作用,仅在订阅版本中。

#2


0  

try this

尝试这个

   /**
   * Converts a CSV file to a simple XML file
 *
 * @param string $file
 * @param string $container
 * @param string $rows
 * @return string
 */
   function csv2xml($file, $container = 'data', $rows = 'row')
{
        $r = "<{$container}>\n";
        $row = 0;
        $cols = 0;
        $titles = array();

        $handle = @fopen($file, 'r');
        if (!$handle) return $handle;

        while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
        {
             if ($row > 0) $r .= "\t<{$rows}>\n";
             if (!$cols) $cols = count($data);
             for ($i = 0; $i < $cols; $i++)
             {
                  if ($row == 0)
                  {
                       $titles[$i] = $data[$i];
                       continue;
                  }

                  $r .= "\t\t<{$titles[$i]}>";
                  $r .= $data[$i];
                  $r .= "</{$titles[$i]}>\n";
             }
             if ($row > 0) $r .= "\t</{$rows}>\n";
             $row++;
        }
        fclose($handle);
        $r .= "</{$container}>";

        return $r;
}

$xml = csv2xml('/home/user/myfile.csv', 'people', 'person');

?>

found this at: http://www.hotscripts.com/listing/convert-a-csv-file-to-xml-using-php/

发现于:http://www.hotscripts.com/listing/convert-a-csv-file-to-xml-using-php/

#3


0  

Here is an XQuery program that should do it:

这是一个应该这样做的XQuery程序:

<entities>
{
  let $rows :=
    let $input := "ID,Name,Age
      1,qwerty,12
      2,asdf,11
      3,zxcvb,10"
    return tokenize($input, "&#10;")
  let $columns := tokenize(head($rows), ",")
  for $row in tail($rows)
  let $data := tokenize($row, ",")
  return 
    <entity>
    {
      for $column at $p in $columns
      return
        <field name="{$column}" value="{$data[$p]}"/>
    }
   </entity>
}
</entities>

Tested on try.zorba-xquery.com, it outputs:

在try.zorba-xquery.com上测试,它输出:

<?xml version="1.0" encoding="UTF-8"?>
<entities>
  <entity>
    <field name="ID" value=" 1"/>
    <field name="Name" value="qwerty"/>
    <field name="Age" value="12"/>
  </entity>
  <entity>
    <field name="ID" value=" 2"/>
    <field name="Name" value="asdf"/>
    <field name="Age" value="11"/>
  </entity>
  <entity>
    <field name="ID" value=" 3"/>
    <field name="Name" value="zxcvb"/>
    <field name="Age" value="10"/>
  </entity>
</entities>