1. XML 2 CSV

Hi,

Anyone know if there is a good library for converting xml 2 csv format? This includes xml tag attribute data..

Kenneth aka ZNorQ

new topic     » topic index » view message » categorize

2. Re: XML 2 CSV

In two steps:

  1. use http://www.rapideuphoria.com/xml_parser2.zip to convert XML string to a sequence
  2. use this to save this sequence as CSV
global function sequence2CSV(sequence section) 
-- converts a sequence to a CSV line 
-- CSV separator is a semicolon 
-- CSV strings are surrounded by quotes 
  integer l 
  sequence line 
 
  if atom(section) then 
    line = to_string(section) 
  else 
    l = length(section) 
    line = {} 
    if l > 0 then 
      for j = 1 to l-1 do 
        if atom(section[j]) or isDate(section[j]) then 
          line &= to_string(section[j])&";" 
        else 
          line &= "\""&to_string(section[j])&"\";" 
        end if 
      end for 
      if atom(section[l]) or isDate(section[l]) then 
        line &= to_string(section[l]) 
      else 
        line &= "\""&to_string(section[l])&"\"" 
      end if 
    end if 
  end if 
  return line 
end function 
 
------------------------------------------------------------------------------ 
 
global procedure saveCSV(sequence filename, sequence section) 
-- saves a sequence in a CSV file 
-- CSV separator is a semicolon 
-- CSV strings are surrounded by quotes 
  integer f_out 
 
  f_out = open(filename, "w") 
  if f_out != -1 then 
    for i = 1 to length(section) do 
      puts(f_out, sequence2CSV(section[i])&"\n") 
    end for 
    close(f_out) 
  end if 
end procedure 
 
------------------------------------------------------------------------------ 
 
global procedure appendCSV(sequence filename, sequence section) 
  integer f_out 
 
  f_out = open(filename, "a") 
  if f_out != -1 then 
    for i = 1 to length(section) do 
      puts(f_out, sequence2CSV(section[i])&"\n") 
    end for 
    close(f_out) 
  end if 
end procedure 

Jean-Marc

new topic     » goto parent     » topic index » view message » categorize

3. Re: XML 2 CSV

Jean-Marc,

I'm trying out the xml parser library, and from what I can understand I must run xml2sequence() function first, then run the data through your sequence2CSV() function...(?) However it uses a to_string() function that I can't find - could you please give me any pointers?

Regards, Kenneth aka ZNorQ

new topic     » goto parent     » topic index » view message » categorize

4. Re: XML 2 CSV

ZNorQ said...

Jean-Marc,

I'm trying out the xml parser library, and from what I can understand I must run xml2sequence() function first, then run the data through your sequence2CSV() function...(?) However it uses a to_string() function that I can't find - could you please give me any pointers?

Regards, Kenneth aka ZNorQ

to_string search

to_string in manual

It's in the std/convert.e lib. You have that?

new topic     » goto parent     » topic index » view message » categorize

5. Re: XML 2 CSV

euphoric said...
ZNorQ said...

Jean-Marc,

I'm trying out the xml parser library, and from what I can understand I must run xml2sequence() function first, then run the data through your sequence2CSV() function...(?) However it uses a to_string() function that I can't find - could you please give me any pointers?

Regards, Kenneth aka ZNorQ

to_string search

to_string in manual

It's in the std/convert.e lib. You have that?

Ups, my bad, I assumed it was part of the library or something. Didn't think that it could be part of the standard libs.

Thanks though.
Kenneth aka ZNorQ

new topic     » goto parent     » topic index » view message » categorize

6. Re: XML 2 CSV

ZNorQ said...

Ups, my bad, I assumed it was part of the library or something. Didn't think that it could be part of the standard libs.

FWIW, I'm glad you queried it (never heard of it either), gave me a reason to add it to Phix.

Pete

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu