Pergunta

I am parsing some XML data and one of the elements contains several URLs that are separated by commas which point to JPG files. How would I go about separating each URL from the string and assigning it to a variable so that I can pull individual pictures? I was thinking NSScanner would work because it scans through strings looking for certain characters but I'm not entirely sure. I also read that regular expressions are a possibility as well. Thoughts on where I should start? Has anyone else needed to separate URLs from a string as well? Also, here is an example of the XML file for reference.

<vehicle>
           <stock>T107810A</stock>
           <year>2009</year>
           <make>Porsche</make>
           <model>Cayenne</model>
           <trim>4WD</trim>
           <color>Black</color>
           <serial>WP1AD29PX9LA64805</serial>
           <miles>38907</miles>
           <cost>0</cost>
           <internet>46241</internet>
           <images>http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_1.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_2.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_3.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_4.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_5.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_6.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_7.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_8.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_9.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_10.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_11.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_12.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_13.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_14.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_15.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_16.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_17.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_18.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_19.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_20.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_21.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_22.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_23.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_24.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_25.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_26.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_27.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_28.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_29.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_30.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_31.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_32.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_33.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_34.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_35.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_36.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_37.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_38.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_39.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_40.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_41.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_42.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_43.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_44.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_45.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_46.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_47.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_48.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_49.jpg,http://cdn.lauc.us/photos/large/WP1AD29PX9LA64805_50.jpg</images>
           <days>612</days>
     </vehicle>
Foi útil?

Solução

From you example, supposing that you did all the rest of the parsing, and you got:

NSString *allURLs = @"http://image1.jpg,http://image2.jpg,http://image3.jpg";

You can get all your URLs in a array (useful for loops) like this:

NSArray *arrayOfImagesURLs = [allURLs componentsSeparatedByString:@","];
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top