我应该如何去将XML转换为CSV [英] how should I go about converting xml into csv

查看:132
本文介绍了我应该如何去将XML转换为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图采取一些XML code,一个样本是如下:

 < time_report> &所述; PROJECT_ID→4&下; / PROJECT_ID> < project_status>关闭< / project_status> <&的client_ID GT; 6001< /&的client_ID GT;     < time_record>            &所述; project_start_time→15:02:33&下; / project_start_time>            &所述; project_end_time→15:07:44&下; / project_end_time>            < project_total_time> PT00H05M11S< / project_total_time>     < / time_record> <&EMPLOYEE_ID GT; 10001< / EMPLOYEE_ID> < employee_name>玛丽·贝丝·< / employee_name> <&DATE_CREATED GT;&2009-08-25 LT; / DATE_CREATED>< / time_report>

,然后输出它,所以它是按以下格​​式:

  PROJECT_ID,project_status,CLIENT_ID,project_start_time,project_end_time,project_total_time,EMPLOYEE_ID,employee_name,DATE_CREATED4,关闭,6001,15时02分33秒,15时07分44秒,PT00H05M11S,10001,玛丽·贝丝·,2009-08-25

我一直在试图用xmllint要做到这一点,但不幸未能取得任何进展,话说回来,我想知道是否有人会有一个建议,以我应该怎么办呢?我会在bash / shell环境这样做。任何帮助将大大pciated AP $ P $,谢谢!

也忘了提,我能得到正确的结果,如果我打开XML在Excel文件了,然后保存为CSV,只是在寻找一种方式来做到这一点在linux

  project_ID,project_status,client_ID,project_start_time,project_end_time,project_total_time,employee_ID,employee_name,date_created
4,关闭,6001,15:02:33,15:07:44,PT00H05M11S,10001,玛丽·贝丝·,8元/二千○九分之二十五
5,开放,6003,12:00:00,12:45:00,PT00H45M00S,10003,米歇尔9/11/2009年
2,关闭,6002,10:00:00,10:30:00,PT00H30M00S,10002,乔,8元/二千○九分之二十五
2,开放,6004,12:00:00,3:27:05,PT03H23M05S,10004,麦克,8元/二千〇九分之一十三


解决方案

xmlstarlet是一个非常强大的命令行工具,它可以让你查​​询XML或运行XSLT转换。这里也有一些XSLT XML-> CSV例子左右浮动,但下面的单行为您提供您所需要的:

  xmlstarlet SEL -B -t -m// time_reports / time_report-n -m*-v。 -o,input.xml中

唯一的问题是,我需要换行< time_report> 用称为根级别标记< time_reports>

I am trying to take some xml code, a sample being below:

  <time_report>

 <project_ID>4</project_ID>

 <project_status>close</project_status>

 <client_ID>6001</client_ID>

     <time_record>

            <project_start_time>15:02:33</project_start_time>

            <project_end_time>15:07:44</project_end_time>

            <project_total_time>PT00H05M11S</project_total_time>

     </time_record>

 <employee_ID>10001</employee_ID>

 <employee_name>Mary Beth</employee_name>

 <date_created>2009-08-25</date_created>

</time_report>

and then output it so it is in the following format:

project_id, project_status, client_id, project_start_time, project_end_time,  project_total_time, employee_ID, employee_name, date_created

4, close, 6001, 15:02:33, 15:07:44, PT00H05M11S, 10001, Mary Beth, 2009-08-25

I have been trying to use xmllint to do this, but have unfortunately not been able to make any progress, having said that I was wondering if anyone would have a suggestion as to what I should do? I would be doing this in a bash/shell environment. any help would be much appreciated, thanks!

also forgot to mention that I can get the correct results if I open the xml file up in excel and then save as csv, just looking for a way to do it in linux

    project_ID,project_status,client_ID,project_start_time,project_end_time,project_total_time,employee_ID,employee_name,date_created
4,close,6001,15:02:33,15:07:44,PT00H05M11S,10001,Mary Beth,8/25/2009
5,open,6003,12:00:00,12:45:00,PT00H45M00S,10003,Michelle,9/11/2009
2,close,6002,10:00:00,10:30:00,PT00H30M00S,10002,Joe,8/25/2009
2,open,6004,12:00:00,3:27:05,PT03H23M05S,10004,Mike,8/13/2009

解决方案

xmlstarlet is a very powerful command line tool which lets you query XML or run XSLT translations. There's some XSLT XML->CSV examples floating around but the following one-liner gives you what you need:

xmlstarlet sel -B -t -m "//time_reports/time_report" -n -m "*" -v . -o , input.xml

The only problem was that I needed to wrap <time_report> with a root level tag called <time_reports>

这篇关于我应该如何去将XML转换为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆