Convert xml file to csv in shell script?

2020-02-29 11:08发布

I'm trying to convert a xml file to a csv file. I have an input xml file like this:

<Row>
  <Cell>
    <Data Type="String" >START</Data>
  </Cell>
  <Cell>
    <Data Type="DateTime" >2013-01-15T21:30:42</Data>
  </Cell>
  <Cell>
    <Data Type="String" ></Data>
  </Cell>
  <Cell>
    <Data Type="String" >Start 'suite8'</Data>
  </Cell>
  <Cell>
    <Data Type="String" >Test 'suite8' started</Data>
  </Cell>
  <Cell>
    <Data Type="String" ></Data>
  </Cell>
</Row>
<Row/>
<Row>
  <Cell>
    <Data Type="String" >START_TEST_CASE</Data>
  </Cell>
  <Cell>
    <Data Type="DateTime" >2013-01-15T21:30:42</Data>
  </Cell>
  <Cell>
    <Data Type="String" ></Data>
  </Cell>
  <Cell>
    <Data Type="String" >Start 'case1'</Data>
  </Cell>
  <Cell>
    <Data Type="String" >Test Case 'case1' started</Data>
  </Cell>
  <Cell>
    <Data Type="String" >case1</Data>
  </Cell>
</Row>

I'm interested in the bits between the tags <Data Type="String" > and </Data>. Also, a new line should be started when the tag <Row> appears.

The output csv file I want should look like this:

START,2013-01-15T21:30:42,,Test 'suite8' started 

START_TEST_CASE,2013-01-15T21:30:42,,Start 'case1',Test Case 'case1' started,case1

I hope this is clear enough, any help is greatly appreciated :) Thanks!

2条回答
神经病院院长
2楼-- · 2020-02-29 11:44

Take a look at xslt stylesheets and the xsltproc command. If it is just converting unconditionally all data to rows with comma separated values from the cell tags it's a relatively simple stylesheet.

A quick search yielded this: XML to CSV Using XSLT With a few adaptations to your xml it should do what you need.

查看更多
smile是对你的礼貌
3楼-- · 2020-02-29 11:55

Parsing XML with Bash has been addressed here before:

How to parse XML in Bash?

That said it seems like a painful way to live.

查看更多
登录 后发表回答