A7Soft xml2csv is a powerful command line tool for converting XML files to CSV files. Usage: xml2csv [-A|-E] [-C=] [-D=] [-F] [-M=] [-N] [-Q[A|B|N|S]] [-R] [-S] [-V[0|1|2]] [-X] Where: xml-file - The source xml file to be converted csv-file - The target csv file to be created fields - String containing names of elements or attributes separated by comma or by character you specified in the -S argument fields-file - The text file containing names of elements or attributes separated by comma or by character you specified in the -S argument -A - Option to convert only xml attributes to csv values -E - Option to convert only xml elements to csv values (defaults: xml2csv converts both elements and attributes) -C - An output file encoding e.g. UTF-16, UTF-8, WINDOWS-1250..WINDOWS-1258, ISO-8859-1..ISO-8859-9 -D - A character to separate csv fields (defaults:comma[,]) or escape sequence \t - tabulation \r - carriage return \n - new line \s - space \# \#09 \#255 \x \x00FF -F - Enable full schema constraint checking processing (defaults:OFF) -M - Maximum amount of rows to write to one CSV file, create next CSV when amount of rows exceeds this value (default:No maximum, one CSV file) -N - Disable namespace processing. (defaults:ON) -Q - Option to enclose csv values by quote and Replace quotes within values to sequences of two quotes ("") (default) -QA - Replace quotes to apostrophes (') -QB - Replace quotes to sequences of back slash and quote (\") -QN - No replacement, leave quotes as is (not recommended) -QS - Replace quotes to sequences of slash and quote (/") -R - Raw text mode leave CR/LF characters unchanged, otherwise convert CR/LF characters to spaces -S - Disable schema processing. (defaults:ON) -V[0|1|2] - Schema validation Auto - V0 Never - V1 Always - V2 (defaults:Auto) -X - Option to append CSV values to existing file (defaults: xml2csv overwrites existing file) Example of command line arguments: xml2csv file.xml file.csv field.txt -Q -D=\t -C=UTF-8 Description: xml2csv converts file.xml to file.csv all fields specified in field.txt file enclosing values by quotes, using tabulation as separator and save output file in UTF-8 encoding By default xml2csv converts both XML elements and XML attributes to CSV, If you want xml2csv to convert only XML attributes please use the -A option. You can use the -E option to convert only XML elements to CSV values. In all cases you have to specify the list of elements or attributes as a text string separated by comma. The list may be in a text file (recommended) or in a command string. NOTE! xml2csv is a case sensitive tool, you should specify the names of the elements exactly as they appear within xml file. If a xml file you want to convert is complicated and has duplicated element names or attributes you can use aliases (see example 5). You can specify a character to separate fields in the csv file by using the -D= argument. In that case use the same character or comma in the field's file (see example 2). Use comma, if you specify an escape sequence in -D argument. Examples The source XML file "elements.xml" has elements and attributes This is an apple Please take it Follow them This is green apple Example 1 xml2csv converts both elements and attributes -------------------------------------------------------- Usage: xml2csv elements.xml elements.csv "uuid,name,price,description,text" CSV file: elements.csv ......................... uuid,name,price,description,text 1,apple,15,This is an apple,Please take it 2,green apple,20,This is green apple,Follow them .......................... Example 2 xml2csv converts only elements and separates fields by semicolon ----------------------------------------- xml2csv elements.xml elements.csv "uuid;name;price;description;text" -E -D=; Or the same result xml2csv elements.xml elements.csv "uuid,name,price,description,text" -E -D=; CSV file: uuid;name;price;description;text ;;;This is an apple;Please take it ;;;This is green apple;Follow them Using escape sequence xml2csv elements.xml elements.csv "uuid,name,price,description,text" -E -D=\#64 CSV file: uuid@name@price@description@text @@@This is an apple@Please take it @@@This is green apple@Follow them Example 3 xml2csv converts only attributes ------------------------------------------- xml2csv elements.xml elements.csv "uuid,name,price,description,text" -A CSV file: uuid,name,price,description,text 1,apple,15,, 2,green apple,20,, Example 4 using fields-file ------------------------------------------- xml2csv elements.xml elements.csv fields.txt where "fields.txt" contains a line "uuid,name,price,description,text" Example 5 duplicate element names and complicated xml structure ---------------------------------------------------------------- Sometimes structure of a xml file is complicated and has duplicated element names or attributes. Jose Carlos Box There are three appearances of "fname" in that xml fragment. The seller element has the "fname" sub-element and the buyer has the "fname" and the assistant element has the "fname" attribute. To extract all fields named "fname" you can use aliases in the field's file. First line of the field's file contains aliases and real names. Each alias and name should be unique, but xml2csv supports duplicate elements. You can mix aliases and simple element and attribute names. Next lines contains definition of each alias. Each definition is on one line. The definition is a line: alias=full-element-or-attribute-name. The full name contains the name of attribute or element and names of all ancestors beginning with the name of the root element. Alias of attribute may contain only the element name and the attribute name separated by '|'. Each name of elements separated by '>' and the name of an attribute separated by '|'. The field file for the example: ----------------------------------------- seller_fname,buyer_fname,assistant_fname_1,assistant_fname_2,product seller_fname=orders>order>seller>fname buyer_fname=orders>order>buyer>fname assistant_fname_1=orders>order>assistant|fname assistant_fname_2=orders>order>assistant|fname ----------------------------------------- The field file have three aliases and one simple element name. You can see the last alias "assistant_name" is an attribute name and has '|' character before attribute name. The name of the "product" element is a simple element name. Example 6 common elements/attributes to be inserted into each row of csv file ---------------------------------------------------------------- Suppose there are some elements or attributes that you want to insert into each row of the csv file. ABCD ZUT56 and a csv file you want to be: client,type,title,name,price "ABCD","FOB","ZUT56","HETZ","34.00" "ABCD","FOB","ZUT56","MJFH","20.00" "ABCD","FOB","ZUT56","HFZG","10.70" "ABCD","FOB","ZUT56","IKRG","25.90" "ABCD","FIS","","HETZ","34.00" "ABCD","FIS","","MJFH","20.00" "ABCD","FIS","","HFZG","10.70" "ABCD","FIS","","IKRG","25.90" The "type" attribute is a common attribute for each item element. You can specify the "type" alias as a common attribute and the "client" element as a common element by using "==" instead of "=". Also you can specify the title alias as common element within parent element scope by using "=*" instead of "==". client,type,title,name,price client==orders>client # common element type==orders>order|type # common attribute title=*orders>order>title # common element within element scope name=orders>order>item|name price=orders>order>item|price Example 7 repeat csv values and specify constant field -------------------------------------- You can repeat some fields several times to get equal csv values For instance "type" will be presented twice if you use type,title,name,price,type type==order|type # common attribute title==order>title # common element name=order>item|name price=order>item|price result csv: type,title,name,price,type "FOB","ZUT56","HETZ","34.00","FOB" "FOB","ZUT56","MJFH","20.00","FOB" "FOB","ZUT56","HFZG","10.70","FOB" "FOB","ZUT56","IKRG","25.90","FOB" To specify field as a constant field you can enclose it in quotes type,title,name,price type="FOB" # constant field title==order>title # common element name=order>item|name price=order>item|price "FOB","ZUT56","HETZ","34.00" "FOB","ZUT56","MJFH","20.00" "FOB","ZUT56","HFZG","10.70" "FOB","ZUT56","IKRG","25.90" ------------------------------------- For more detail please visit our site http://www.a7soft.com