0
<cids>
<defect name="29237" action="decided" checker="OVERRUN_STATIC" cid="29237"/>
<defect ....

</cids>

I want to convert this xml to CSV I checked many links but didn't find any link where XML is converted to CSV by parsing attributes.

  • It might be overkill for simple xml files but you could deserialize your xml file first to java classes (http://stackoverflow.com/questions/12454240/generate-java-class-from-xml-file-using-xstream) and then just write all fields to a csv file. – HectorLector Nov 05 '13 at 13:31
  • or using xslt you can do it. – vels4j Nov 05 '13 at 13:34

2 Answers2

2

Windows command line

for /F tokens^=2^,4^,6^,8^ delims^=^" %a in ('type data.xml ^| find "<defect" ') do echo %a,%b,%c,%d >> data.csv

AWK (in this case, also from windows command line)

awk -F \" -v OFS=, "/^<defec/{print $2,$4,$6,$8}" data.xml > data.csv

In both cases, using quotes as delimiter to split records into fields.

MC ND
  • 69,615
  • 8
  • 84
  • 126
1

As no language or anything was asked, this is a vim command (tested on vim 7.3 for windows)

%s/<defect \w\+="\(.\+\)" \w\+="\(.\+\)" \w\+="\(.\+\)" \w\+="\(.\+\)"\/>/\1,\2,\3,\4/g

THEN, delete the first and last line by hand.

aaronps
  • 324
  • 1
  • 6