This question already has an answer here:

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[
    year], "XML"];

I tried to put Dynamic[year] instead of year but it crashes en got this error:

"String expected at position 2 in


My question is why StringJoin put '<>' in the url in the error message. I used the same function before and it worked, any ideas?


marked as duplicate by Sjoerd C. de Vries, dr.blochwave, Mr.Wizard Jul 22 '15 at 11:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ 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). $\endgroup$ – Sjoerd C. de Vries Jul 22 '15 at 10:24
  • $\begingroup$ 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 $\endgroup$ – Alberto Jul 22 '15 at 10:31
  • $\begingroup$ 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. $\endgroup$ – Sjoerd C. de Vries Jul 22 '15 at 10:34
  • $\begingroup$ reference.wolfram.com/language/ref/StringJoin.html reference.wolfram.com/language/ref/Join.html 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 $\endgroup$ – Alberto Jul 22 '15 at 10:38
  • $\begingroup$ 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? $\endgroup$ – Sjoerd C. de Vries Jul 22 '15 at 10:43

Moving it to a Manipulate sounds like a better idea:

 makes = Import[
    ToString@year], "XML"];
 Row[{"Loaded car makes for the year ", 
   Style[ToString[year], {FontFamily -> "Arial", Large, Red}]}],
 {year, Reverse@years}

Mathematica graphics

  • $\begingroup$ Okey thanks it works $\endgroup$ – Alberto Jul 22 '15 at 11:07
  • $\begingroup$ if I want to put make at other dropdowm and update data depend of year selected: i have to make {make, Reverse@makes}, or its impossible? $\endgroup$ – Alberto Jul 22 '15 at 11:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.