0

我有一个csv:

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

我需要创建一个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>

列名因一个 csv 文件而异。所以我想做一个通用的工作来接受所有的列并生成一个如图所示的 xml。任何帮助,将不胜感激

4

3 回答 3

1

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

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

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

于 2011-10-10T16:53:27.010 回答
0

尝试这个

   /**
   * 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');

?>

在以下位置找到:http: //www.hotscripts.com/listing/convert-a-csv-file-to-xml-using-php/

于 2011-10-10T13:55:57.763 回答
0

这是一个应该执行此操作的 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>

在 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>
于 2011-10-10T14:17:19.683 回答