【问题标题】:Possible use of NSScanner to separate data可能使用 NSScanner 来分离数据
【发布时间】:2014-04-09 12:41:01
【问题描述】:

我正在解析一些XML 数据,其中一个元素包含几个URLs,它们由指向JPG 文件的逗号分隔。我将如何将每个 URL 从字符串中分离出来并将其分配给一个变量,以便我可以提取单独的图片?我在想NSScanner 会起作用,因为它会扫描字符串以查找某些字符,但我不完全确定。我还读到正则表达式也是一种可能性。关于我应该从哪里开始的想法?还有其他人需要将URLs 与字符串分开吗?另外,这里有一个XML 文件的示例供参考。

<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>

【问题讨论】:

    标签: xml url jpeg nsscanner


    【解决方案1】:

    从您的示例中,假设您完成了所有其余的解析,并且您得到:

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

    您可以像这样在数组中获取所有 URL(对于循环很有用):

    NSArray *arrayOfImagesURLs = [allURLs componentsSeparatedByString:@","];
    

    【讨论】:

    • 很棒的答案。谢谢@Larme
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    相关资源
    最近更新 更多