csv to xml: not sure the best way to do it in Mule ESB

吃可爱长大的小学妹 提交于 2019-12-20 06:15:27

问题


I'm new to Mule, so bear with me. I have the following CSV file that I receive:

Company1,2,123 Street,Winchester,UK
"000010","CHRISTINE","I","HAAS","A00","3978","1995-01-01","PRES",18,"F","1963-08-24",152750.00
"000020","MICHAEL","L","THOMPSON","B01","3476","2003-10-10","MANAGER",18,"M","1978-02-02",94250.00

The first line, header, contains company info plus the number of records (number of employees) in CSV file (second parm in the header).

Now I need to convert it to the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<tns:employeedata xmlns:tns="http://coxb.test.legstar.com/payrollemployee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://coxb.test.legstar.com/payrollemployee PayrollEmployee.xsd ">
  <tns:employeecount>2</tns:employeecount>
  <tns:employeelist>
    <tns:employees>
      <tns:employeenumber>000010</tns:employeenumber>
      <tns:firstname>CHRISTINE</tns:firstname>
      <tns:middleinitial>I</tns:middleinitial>
      <tns:surname>HAAS</tns:surname>
      <tns:department>A00</tns:department>
    </tns:employees>
    <tns:employees>
      <tns:employeenumber>000020</tns:employeenumber>
      <tns:firstname>MICHAEL</tns:firstname>
      <tns:middleinitial>L</tns:middleinitial>
      <tns:surname>THOMPSON</tns:surname>
      <tns:department>B01</tns:department>
    </tns:employees>
  </tns:employeelist>
</tns:employeedata>

I could easily transform this file without the first line (header). My problem is how to process the header and extract/transform "employeecount".

Any help will be greatly appreciated.


回答1:


The easiest way to do this is to use DataMapper. Set the input to CSV (using a sample CSV) and the output to XML (using your XSD or a sample XML).

Once you're in the mapping view, click on your employeecount field. You'll see an area where you can enter an expression. There is a non-documented parameter $in.0.__id which you can use which will contain the record count. Note that this will only work for CSV files.

Regarding how to skip the first line, DataMapper does this by default.



来源:https://stackoverflow.com/questions/15301420/csv-to-xml-not-sure-the-best-way-to-do-it-in-mule-esb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!