• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

xml or csv? when it comes to importing data...

New Here ,
Jun 30, 2009 Jun 30, 2009

Copy link to clipboard

Copied

I need to import some dynamic values.  I was curious to know if xml or csv was better suited with javascript...  has anyone had a better experience with either of the formats?  Thanks again,

- Jacques

TOPICS
Actions and scripting

Views

822

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advisor , Jun 30, 2009 Jun 30, 2009

If you're familiar at all with XML and your data has any complexity, use XML.

If the data is simply a table of values, use CSV.

If the data is an arbitrary set of key/value pairs, I prefer to use .ini files. They are easy to read/write in JS and by humans.

-X

Votes

Translate

Translate
Adobe
Advisor ,
Jun 30, 2009 Jun 30, 2009

Copy link to clipboard

Copied

If you're familiar at all with XML and your data has any complexity, use XML.

If the data is simply a table of values, use CSV.

If the data is an arbitrary set of key/value pairs, I prefer to use .ini files. They are easy to read/write in JS and by humans.

-X

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 30, 2009 Jun 30, 2009

Copy link to clipboard

Copied

Thanks, managed to get it working using csv.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jul 01, 2009 Jul 01, 2009

Copy link to clipboard

Copied

LATEST

FWIW, here's some code from stdlib.js for parsing lines in a CSV file. This correctly handles quoted fields (with embedded commas) and just about everything else that I've been able to throw at it. Some of the code (the 'while (true)' loop, in particular) is there because I couldn't get the master RegExp expression quite right and had to handle some cases manually.

  var rexStr = '([^",]+)|"((?:[^"]|"")*)"|^,';

  var rexp = new RegExp(rexStr);

  function parseCSVLine(line) {

    var ch = ',';
    var parts = [];
    var res;

    while (line.length && (res = line.match(rexp)) != null) {
      if (res[1] || res[2]) {
        if (res[1]) {
          parts.push(res[1]);
        } else {
          parts.push(res[2].replace(/""/g, '"'));
        }
        line = line.slice(res[0].length + res.index);
        if (line[0] == ch) {
          line = line.slice(1);
        }
      } else {
        while (true) {
          if (line[0] == ch) {
            parts.push('');
            line = line.slice(1);
            continue;
          }
          if (line.startsWith('""')) {
            parts.push('');
            line = line.slice(2);
            if (line[0] == ch) {
              line = line.slice(1);
            }
            continue;
          }
          break;
        }
      }
    }
    return parts;
  }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines