My goal is to import a XML file from the site fueleconomy.gov to get a list of cars dependent on the year given as the user input .
Here is the code:
years = Import["http://www.fueleconomy.gov/ws/rest/vehicle/menu/year", "XML"];
y = Cases[years, XMLElement["menuItem", _, _], Infinity] /.
XMLElement[_, _, {val_}] -> val /.
XMLElement["menuItem", _, list_] -> list;
Do[y[[i]] = DeleteDuplicates[y[[i]]], {i, 1, Length[y], 1}];
years = y[[All, 1]]
PopupMenu[Dynamic[year], years]
makes = Import[
StringJoin[
"http://www.fueleconomy.gov/ws/rest/vehicle/menu/make?year=",
year], "XML"];
I tried to put Dynamic[year]
instead of year but it crashes en got this error:
"String expected at position 2 in
http://www.fueleconomy.gov/ws/rest/vehicle/menu/make?year=<>2014"
My question is why StringJoin
put '<>' in the url in the error message. I used the same function before and it worked, any ideas?
Dynamic
puts an invisible wrapper around its argument so thatStringJoin
does not see it as a String anymore.year
itself is a number and no String, so you should useToString
on it. The<>
in the error message is simply the infix form ofStringJoin
(see its doc page). $\endgroup$ – Sjoerd C. de Vries Jul 22 '15 at 10:24makes
variable updated automatically when the user selects a new year from the pop-up menu? $\endgroup$ – Sjoerd C. de Vries Jul 22 '15 at 10:43