#C13835. CSV to XML Conversion
CSV to XML Conversion
CSV to XML Conversion
In this problem, you are given the contents of a CSV file through standard input. The CSV file starts with a header row, followed by one or more rows of data. Your task is to convert the CSV data into an XML format.
The conversion format is as follows:
- The root element should be
<Root>
. - Each row of the CSV (after the header) should be wrapped into an
<Entry>
element. - For every column, create an element using the header name as the tag and the corresponding field as the element's text content.
Note that the CSV file may contain quoted fields that include commas. Your solution must handle such cases correctly. All input is provided via standard input and the output (the XML string) should be printed to standard output.
Input Example:
id,name,age 1,John Doe,30 2,Jane Smith,25
Output Example:
<Root><Entry><id>1</id><name>John Doe</name><age>30</age></Entry><Entry><id>2</id><name>Jane Smith</name><age>25</age></Entry></Root>
inputFormat
The input is provided via standard input and consists of multiple lines representing the contents of a CSV file. The first line is the header line and subsequent lines contain the data rows. Fields may be enclosed in double quotes if they contain commas.
outputFormat
The output should be a single XML string printed to standard output. The XML string should have a root element <Root>
and each CSV row should be transformed into an <Entry>
element with child elements corresponding to the CSV headers.
id,name,age
1,John Doe,30
2,Jane Smith,25
1John Doe302Jane Smith25