1

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?

Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323
Alberto
  • 89
  • 4
  • The problem is that Dynamic puts an invisible wrapper around its argument so that StringJoin does not see it as a String anymore. year itself is a number and no String, so you should use ToStringon it. The <> in the error message is simply the infix form of StringJoin (see its doc page). – Sjoerd C. de Vries Jul 22 '15 at 10:24
  • Yes, I watched it, the solution gived by the page don't work it says that ToString[exp] solved it but if I do that with my variable it return Dynamic[year] and no the value – Alberto Jul 22 '15 at 10:31
  • Which page are you referring to? As I wrote in my above comment you cant use Dynamic[year] where a string is expected as that simply isn't a string. If I replace year in your last line with ToString[year] the code works. – Sjoerd C. de Vries Jul 22 '15 at 10:34
  • https://reference.wolfram.com/language/ref/StringJoin.html https://reference.wolfram.com/language/ref/Join.html https://reference.wolfram.com/language/ref/Flatten.html

    I see this references and year is a string the xml file have two elements text and value and i get text Also i try to put Dynamic[..] englobing makes =Import[...] and it works

    – Alberto Jul 22 '15 at 10:38
  • You don't write this in your question, but is your intention to have the makes variable updated automatically when the user selects a new year from the pop-up menu? – Sjoerd C. de Vries Jul 22 '15 at 10:43
  • Sorry, yes that's my idea – Alberto Jul 22 '15 at 10:47
  • Well, have a look at my answer below then. – Sjoerd C. de Vries Jul 22 '15 at 10:57
  • Please see specifically: (18393). Many people misunderstand Dynamic. I was surely one of them. It is a true "pitfall" IMHO. – Mr.Wizard Jul 22 '15 at 11:17

1 Answers1

1

Moving it to a Manipulate sounds like a better idea:

Manipulate[
 makes = Import[
   StringJoin[
    "http://www.fueleconomy.gov/ws/rest/vehicle/menu/make?year=", 
    ToString@year], "XML"];
 Row[{"Loaded car makes for the year ", 
   Style[ToString[year], {FontFamily -> "Arial", Large, Red}]}],
 {year, Reverse@years}
 ]

Mathematica graphics

Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323