Linux csv to xml

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Command-line utility for converting of CSV (comma-separated values) data to an XML representaion

ki11men0w/csv2xml

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Utility for converting CSV (comma separated values) files to XML representation.

The easiest way to build this project is to use the stack utility. This is one of the most common tools for building projects written in Haskell. To install stack , follow the instructions from the official project website.

After the stack is installed, the project can be built and installed like this:

After this you can use csv2xml command line utility:

$ csv2xml --help=100 CSV to XML converter version 0.1.0.0 (haskell) csv2xml [OPTIONS] [INPUT_FILE [OUTPUT_FILE]] Common flags: -i --input-encoding=ENC Encoding of input CSV file. If not specified then default system encoding will be used -o -e --output-encoding=ENC Encoding of resulting XML file. If not specified then UTF-8 will be used as default -r --record-tag-name=TAG_NAME Name for the record XML elements. If not specified the name of `r` will be used -f --field-tag-name=TAG_NAME Name for the field XML elements. If not specified the name of `f` will be used -x --indexed-fields By default all field elements are created with the same name. Use this option to add index number suffix to the field names -n --namespace=XML_NAME_SPACE XML namespace for all elements of the output XML document. By default no namespace is used. -t --tab-delimited Specify this option if the fields in the input CSV file are separated by a tab character. By default, it is assumed that the fields are separated by a comma -s --store-records-source If specified, each record will be supplemented with an additional element `sourceRecord`, which will contain the source text of the corresponding CSV record -? --help Display help message -V --version Print version information --numeric-version Print just the version number Converts CSV file to XML representation. If an INPUT/OUTPUT file is not specified, STDIN/STDOUT will be used, respectively. You can also explicitly specify the use of standard IO streams: to do this, you need to specify a hyphen ("-") instead of the file names.

About

Command-line utility for converting of CSV (comma-separated values) data to an XML representaion

Читайте также:  Defender g lens 323 linux

Источник

How can I convert a CSV file to XML?

I don’t understand. The three format you mention are plain text files — the difference is in content. And scope. Would you mind to add an example?

5 Answers 5

When you know the format of the csv file and the structure you need in the xml file, it’s fairly straightforward to make a script that can handle the conversion.

Jack,35,United States Jill,22,United Kingdom 

You can create the following xml file:

   Jack 35 United States  Jill 22 United Kingdom   

With the following script:

#!/bin/bash file_in="simple.csv" file_out="simple.xml" echo '' > $file_out echo '' >> $file_out while IFS=$',' read -r -a arry do echo ' ' >> $file_out echo ' '$'' >> $file_out echo ' '$'' >> $file_out echo ' '$'' >> $file_out echo ' ' >> $file_out done < $file_in echo '' >> $file_out 

Even if you have never coded before, I think this should be easy to use and modify. The file is read line-by-line in the while loop.

IFS is the internal field specifier. The IFS=$’,’ declares that the value of the field separator is a comma. This is standard for a CSV file, but it can be changed as needed to match the input file format.

The -r argument to the read command tells it to treat any backslashes in your file as part of your data rather than as an escape for a following special character.

The -a arry argument places each column of your file into an array (named arry ). The columns in this example are name, age, country. In other words the values between the commas. So each column in the line is stored in an array.

Читайте также:  Internal network virtualbox linux

Then the needed text for xml is just wrapped around the values and the xml line is appended to the output file with echo .

Источник

csv2xmlcmd — CSV to XML Converter¶

You must pass the tool either a file path, or a web url and you must use the -i or —infile parameter followed by the input as shown in the below. In the first example, we are loading a .json file that is saved in the c:data folder. In the second example, we are loading directly from an example website URL:

csv2xmlcmd -i c:\data\example.csv csv2xmlcmd -i http://www.example.com/example.csv`

-o –outfile¶

The -o or —outfile parameter sets the file in which to save the converted results. If no parameter is set, then the output is written to the screen:

csv2xmlcmd -i c:\example.csv -o c:\converted.xml csv2xmlcmd -i http://www.example.com/example.csv -o c:\converted.xml 

-e –encoding¶

In order to specify an encoding type for the file, pass the -e or —encoding parameter. The most common encoding types are UTF-8 and UTF-16. By default the encoding will be determined for you.

csv2xmlcmd -i c:\data\example.csv -e utf-8 csv2xmlcmd -i c:\data\example.csv --encoding utf-16 csv2xmlcmd -i c:\data\example.csv 

-N –noheader¶

Pass this to indicate the input CSV file has no column headings. The default value is to assume headers are in the input file.

csv2xmlcmd -i c:\example.csv -o c:\converted.xml -N 

-c –config¶

The configuration file used in the conversion is either created at runtime, or passed to the converter before the conversion begins. The -c or —config flag will load a configuration file to used if a path is passed:

csv2xmlcmd -i c:\example.csv -o c:\myoutput.xml -c c:\myconfig.txt 

-P –pretty¶

The -P or —pretty flag will format the output into a pretty format:

csv2xmlcmd -i c:\example.csv -P 

-d –delimiter¶

The -D or —delimiter flag will allow the user to pass the type of delimiter in the CSV file. If not passed, then the delimiter will be determined for you.:

csv2xmlcmd -i c:\example.csv -d '-' 

In order to pass Tab as the delimiter, use -T as the parameter to override the delimiter:

csv2xmlcmd -i c:\example.csv -T 

-f –fields¶

The -f or —fields flag allows you to specifically set which field columns to include:

csv2xmlcmd -i c:\example.csv -f 3,2,1 

-ex –exclude¶

The -ex or —exclude flag allows you to specifically set which field columns to exclude:

csv2xmlcmd -i c:\example.csv" -ex 3-5 

-t –topname¶

The -t or —topname flag sets the name of top XML element. The default is root:

csv2xmlcmd -i c:\example.csv" -t basic 

-r –rowname¶

The -r or —rowname flag sets the name of repeating row element. The default is row:

csv2xmlcmd -i c:\example.csv" -r fieldname 

-l –limit¶

The -l or —limit flag will limit the number of rows used in the CSV file. If no parameter is passed, the converter will write the entire file. This value must be a number. This parameter will not affect the headers.

csv2xmlcmd -i c:\example.csv -l 10 

-V –verbose¶

The -V or —verbose flag will output useful messages as the program runs:

csv2xmlcmd -i c:\example.csv -V 

Источник

Читайте также:  Internal network virtualbox linux

script to convert Csv to xml

Need to process the file using for loop I have written below code to convert csv to xml. Here have written separate tag for each column.
In input file have column from 1 to 278. In output file need to have tag from A1 to A278, Code :

file_in="Prepaid_plan_voucher.csv" file_out="Prepaid_plan_voucher.xml" echo '' > $file_out #echo '' >> $file_out echo ' ' >> $file_out echo ' ' >> $file_out while IFS=$',' read -r -a arry do # echo ' ' >> $file_out # echo ' ' >> $file_out echo ' '$'' >> $file_out echo ' '$'' >> $file_out echo ' '$'' >> $file_out # echo ' ' >> $file_out # echo ' ' >> $file_out done < $file_in #echo '' >> $file_out echo ' ' >> $file_out echo ' ' >> $file_out 

Sample Input file.(this is a sample record in actual input file will contain 278 columns). If input file has two or three records, same needs to be appended in one XML file.

name,Tariff Summary,Record ID No.,Operator Name,Circle (Service Area),list Prepaid Plan Voucher,test_All calls 2p/s,TT07PMPV0188,Ta Te,Gu, Prepaid Plan Voucher,test_All calls 3p/s,TT07PMPV0189,Ta Te,HR, 

Sample output file The above two TariffRecords, tariff will be hard coded at the beginning and end of xml file.

Prepaid Plan Voucher test_All calls 2p/s TT07PMPV0188 Ta Te Gu Prepaid Plan Voucher test_All calls 3p/s TT07PMPV0189 Ta Te HR

Источник

Оцените статью
Adblock
detector