iallen
YaBB Newbies
Offline
Posts: 2
merging a csv parse with a returned xml parse
Oct 14th , 2009, 9:49am
Hello all, I have two bits of code working separately, but having trouble merging them. The first takes all of the addresses in a .csv file and translates them into URL's for Google Maps API, which spits out an XML. (this is from the visualizing data book) see below...void setup() { String[] lines = loadStrings("KE.csv"); parse(lines); } void parse(String[] lines) { for (int i = 0; i < lines.length; i++) { String[] pieces = splitLine(lines[i]); } } String[] splitLine(String line) { char[] c = line.toCharArray(); ArrayList pieces = new ArrayList(); int prev = 0; boolean insideQuote = false; for (int i = 0; i < c.length; i++) { if (c[i] == ',') { if (!insideQuote) { // whitespace must be trimmed between commas. String s = new String(c, prev, i - prev).trim(); pieces.add(s); prev = i+1; } } else if (c[i] == '\"') { insideQuote = !insideQuote; } } if (prev != c.length) { String s = new String (c, prev, c.length - prev).trim(); pieces.add(s); } String[] outgoing = new String [pieces.size()]; pieces.toArray(outgoing); scrubQuote(outgoing); return outgoing; } // parse quotes from csv data. quotes around a column are common, // and actual double quotes (") are specifidd by two double quotes ("") void scrubQuote(String[] array) { for (int i = 0; i < array.length; i++) { if (array[i].length() > 2) { // remove quotes at start and end, if present. if (array[i].startsWith("\"") && array[i].endsWith("\"")) { array[i] = array[i].substring(1, array[i].length() - 1); } } // make double quotes into single quotes. array[i] = array[i].replaceAll("\"\"", "a"); //println(array[i]); } String location = array[1]+", "+array[2]+", "+array[3]; String API_KEY = "ABQIAAAAtGq91BxxuUPgqbhc5R69SRS-e5dD3uKiFkHFcYN6PmNbkD08thRfYWkSfs7y86Dma-WBab2 8638JIw"; String locationEncoded = URLEncoder.encode(location); String url = "({http inserted here}://maps.google.com/maps/geo?q=" + locationEncoded + "&output=xml&oe=utf8&sensor=false&key=" + API_KEY; println(url); } The Second reads the xml returned from the encoded url and prints a SQL statement for database entry (initially the API_KEY, location, locationEncoded etc strings were on the top of this next batch of code, but have since been moved to the code above (as that is the last thing I can get working, everything below errors when I include it. I imagine I need to do some sort of loop to go through the arrays and create a bunch of xml objects, but I am new to all this and it's a bit fuzzy, Here is the second bit of code. Any help would be greatly appreciated, thanks so much!XMLElement coordsElement = xml.getChild("Response/Placemark/Point/coordinates"); String coordsStr = coordsElement.getContent(); String[] coords = split(coordsStr, ','); float lon = parseFloat(coords[0]); float lat = parseFloat(coords[1]); println("INSERT INTO `markers` (`name`, `address`, `lat`, `lng`) VALUES ('"+ location + "," + "'" + lat + "'" + "," + "'" + lon +"'"+");");