2014-02-17 75 views
0

我想創建一個iOS應用其中(除其他一些東西)需要當我使用getPosts從WordPress的的功能在WordPress的頁解析WordPress的XML的iOS/Objective-C的

的內容加載XML-RPC的功能,我得到下面的返回:

<?xml version="1.0" encoding="UTF-8"?> 
<methodResponse> 
    <params> 
    <param> 
     <value> 
     <array><data> 
    <value><struct> 
    <member><name>post_id</name><value><string>23</string></value></member> 
    <member><name>post_title</name><value><string>&lt;!--:de--&gt;Post1&lt;!--:--&gt;&lt;!--:en--&gt;Post1&lt;!--:--&gt;</string></value></member> 
    <member><name>post_date</name><value><dateTime.iso8601>20140211T14:26:39</dateTime.iso8601></value></member> 
    <member><name>post_date_gmt</name><value><dateTime.iso8601>20140211T12:26:39</dateTime.iso8601></value></member> 
    <member><name>post_modified</name><value><dateTime.iso8601>20140217T22:32:45</dateTime.iso8601></value></member> 
    <member><name>post_modified_gmt</name><value><dateTime.iso8601>20140217T20:32:45</dateTime.iso8601></value></member> 
    <member><name>post_status</name><value><string>publish</string></value></member> 
    <member><name>post_type</name><value><string>post</string></value></member> 
    <member><name>post_name</name><value><string>im-notfall</string></value></member> 
    <member><name>post_author</name><value><string>1</string></value></member> 
    <member><name>post_password</name><value><string></string></value></member> 
    <member><name>post_excerpt</name><value><string></string></value></member> 
    <member><name>post_content</name><value><string>&lt;!--:de--&gt;&lt;b&gt;Post1&lt;/b&gt;&lt;!--:--&gt;&lt;!--:en--&gt;&lt;b&gt;Post1&lt;/b&gt;&lt;!--:--&gt;</string></value></member> 
    <member><name>post_parent</name><value><string>0</string></value></member> 
    <member><name>post_mime_type</name><value><string></string></value></member> 
    <member><name>link</name><value><string>http://example.com/ExampleProjekt/?p=23</string></value></member> 
    <member><name>guid</name><value><string>http://example.com/ExampleProjekt/?p=23</string></value></member> 
    <member><name>menu_order</name><value><int>5</int></value></member> 
    <member><name>comment_status</name><value><string>closed</string></value></member> 
    <member><name>ping_status</name><value><string>open</string></value></member> 
    <member><name>sticky</name><value><boolean>0</boolean></value></member> 
    <member><name>post_thumbnail</name><value><array><data> 
</data></array></value></member> 
    <member><name>post_format</name><value><string>standard</string></value></member> 
    <member><name>terms</name><value><array><data> 
    <value><struct> 
    <member><name>term_id</name><value><string>1</string></value></member> 
    <member><name>name</name><value><string>Allgemein</string></value></member> 
    <member><name>slug</name><value><string>allgemein</string></value></member> 
    <member><name>term_group</name><value><string>0</string></value></member> 
    <member><name>term_taxonomy_id</name><value><string>1</string></value></member> 
    <member><name>taxonomy</name><value><string>category</string></value></member> 
    <member><name>description</name><value><string></string></value></member> 
    <member><name>parent</name><value><string>0</string></value></member> 
    <member><name>count</name><value><int>3</int></value></member> 
</struct></value> 
</data></array></value></member> 
    <member><name>custom_fields</name><value><array><data> 
</data></array></value></member> 
</struct></value> 
    <value><struct> 
    <member><name>post_id</name><value><string>9</string></value></member> 
    <member><name>post_title</name><value><string>&lt;!--:de--&gt;Post2&lt;!--:--&gt;&lt;!--:en--&gt;Post2&lt;!--:--&gt;</string></value></member> 
    <member><name>post_date</name><value><dateTime.iso8601>20140206T13:16:56</dateTime.iso8601></value></member> 
    <member><name>post_date_gmt</name><value><dateTime.iso8601>20140206T11:16:56</dateTime.iso8601></value></member> 
    <member><name>post_modified</name><value><dateTime.iso8601>20140217T22:33:01</dateTime.iso8601></value></member> 
    <member><name>post_modified_gmt</name><value><dateTime.iso8601>20140217T20:33:01</dateTime.iso8601></value></member> 
    <member><name>post_status</name><value><string>publish</string></value></member> 
    <member><name>post_type</name><value><string>post</string></value></member> 
    <member><name>post_name</name><value><string>neuer-erster-beitrag</string></value></member> 
    <member><name>post_author</name><value><string>1</string></value></member> 
    <member><name>post_password</name><value><string></string></value></member> 
    <member><name>post_excerpt</name><value><string></string></value></member> 
    <member><name>post_content</name><value><string>&lt;!--:de--&gt;&lt;b&gt;Post2&lt;/b&gt;&lt;!--:--&gt;&lt;!--:en--&gt;&lt;b&gt;Post2&lt;/b&gt;&lt;!--:--&gt;</string></value></member> 
    <member><name>post_parent</name><value><string>0</string></value></member> 
    <member><name>post_mime_type</name><value><string></string></value></member> 
    <member><name>link</name><value><string>http://example.com/ExampleProjekt/?p=9</string></value></member> 
    <member><name>guid</name><value><string>http://example.com/ExampleProjekt/?p=9</string></value></member> 
    <member><name>menu_order</name><value><int>3</int></value></member> 
    <member><name>comment_status</name><value><string>closed</string></value></member> 
    <member><name>ping_status</name><value><string>open</string></value></member> 
    <member><name>sticky</name><value><boolean>0</boolean></value></member> 
    <member><name>post_thumbnail</name><value><array><data> 
</data></array></value></member> 
    <member><name>post_format</name><value><string>standard</string></value></member> 
    <member><name>terms</name><value><array><data> 
    <value><struct> 
    <member><name>term_id</name><value><string>1</string></value></member> 
    <member><name>name</name><value><string>Allgemein</string></value></member> 
    <member><name>slug</name><value><string>allgemein</string></value></member> 
    <member><name>term_group</name><value><string>0</string></value></member> 
    <member><name>term_taxonomy_id</name><value><string>1</string></value></member> 
    <member><name>taxonomy</name><value><string>category</string></value></member> 
    <member><name>description</name><value><string></string></value></member> 
    <member><name>parent</name><value><string>0</string></value></member> 
    <member><name>count</name><value><int>3</int></value></member> 
</struct></value> 
</data></array></value></member> 
    <member><name>custom_fields</name><value><array><data> 
</data></array></value></member> 
</struct></value> 
    <value><struct> 
    <member><name>post_id</name><value><string>5</string></value></member> 
    <member><name>post_title</name><value><string>&lt;!--:de--&gt;Post3&lt;!--:--&gt;&lt;!--:en--&gt;Post3&lt;!--:--&gt;</string></value></member> 
    <member><name>post_date</name><value><dateTime.iso8601>20131217T17:32:09</dateTime.iso8601></value></member> 
    <member><name>post_date_gmt</name><value><dateTime.iso8601>20131217T15:32:09</dateTime.iso8601></value></member> 
    <member><name>post_modified</name><value><dateTime.iso8601>20140217T22:33:18</dateTime.iso8601></value></member> 
    <member><name>post_modified_gmt</name><value><dateTime.iso8601>20140217T20:33:18</dateTime.iso8601></value></member> 
    <member><name>post_status</name><value><string>publish</string></value></member> 
    <member><name>post_type</name><value><string>post</string></value></member> 
    <member><name>post_name</name><value><string>test-beitrag-2</string></value></member> 
    <member><name>post_author</name><value><string>1</string></value></member> 
    <member><name>post_password</name><value><string></string></value></member> 
    <member><name>post_excerpt</name><value><string></string></value></member> 
    <member><name>post_content</name><value><string>&lt;!--:de--&gt;&lt;b&gt;Post3&lt;/b&gt;&lt;!--:--&gt;&lt;!--:en--&gt;&lt;b&gt;Post3&lt;/b&gt;&lt;!--:--&gt;</string></value></member> 
    <member><name>post_parent</name><value><string>0</string></value></member> 
    <member><name>post_mime_type</name><value><string></string></value></member> 
    <member><name>link</name><value><string>http://example.com/ExampleProjekt/?p=5</string></value></member> 
    <member><name>guid</name><value><string>http://example.com/ExampleProjekt/?p=5</string></value></member> 
    <member><name>menu_order</name><value><int>4</int></value></member> 
    <member><name>comment_status</name><value><string>closed</string></value></member> 
    <member><name>ping_status</name><value><string>open</string></value></member> 
    <member><name>sticky</name><value><boolean>0</boolean></value></member> 
    <member><name>post_thumbnail</name><value><array><data> 
</data></array></value></member> 
    <member><name>post_format</name><value><string>standard</string></value></member> 
    <member><name>terms</name><value><array><data> 
    <value><struct> 
    <member><name>term_id</name><value><string>1</string></value></member> 
    <member><name>name</name><value><string>Allgemein</string></value></member> 
    <member><name>slug</name><value><string>allgemein</string></value></member> 
    <member><name>term_group</name><value><string>0</string></value></member> 
    <member><name>term_taxonomy_id</name><value><string>1</string></value></member> 
    <member><name>taxonomy</name><value><string>category</string></value></member> 
    <member><name>description</name><value><string></string></value></member> 
    <member><name>parent</name><value><string>0</string></value></member> 
    <member><name>count</name><value><int>3</int></value></member> 
</struct></value> 
</data></array></value></member> 
    <member><name>custom_fields</name><value><array><data> 
</data></array></value></member> 
</struct></value> 
</data></array> 
     </value> 
    </param> 
    </params> 
</methodResponse> 

具體來說,我需要以下字段:

  • POST_ID
  • POST_TITLE
  • post_motified
  • POST_CONTENT
  • menu_order

我已經嘗試了幾個選項來實現這一目標。 其中一個使用Gdata,在另一篇文章中已經推薦過,但這個例子是爲了更簡單的xml,我似乎無法讓它爲我工作。

NSArray *tempPosts = [XMLdoc nodesForXPath:@"//methodResponse/params/param/value/array/data/value/struct/member" error:nil]; 

我也使用WPXMLRPC框架試圖 - >https://github.com/wordpress-mobile/wpxmlrpc

使用此代碼:

WPXMLRPCDecoder *decodedWPXML = [[WPXMLRPCDecoder alloc] initWithData:XMLcontent]; 
if ([decodedWPXML isFault]) { 
    NSLog(@"XML-RPC error %ld: %@", (long)[decodedWPXML faultCode], [decodedWPXML faultString]); 
} else { 
    NSLog(@"XML-RPC response: %@", [decodedWPXML object]); 
} 

我管理接收的對象,這是我可以通過的NSLog輸出(@「%@ 「,物); 但我無法進一步處理我以這種方式收到的任何數據。 我是(對某些人來說很明顯)對於objective-c來說很新穎。 我也看過官方WordPress的iOS應用程序,但我無法使用任何代碼。

任何幫助將不勝感激,我不介意使用任何不同的框架/技術等,如果他們幫助我的解決方案。

回答

0
GDataXMLDocument *XMLdoc = [[GDataXMLDocument alloc] initWithData:XMLcontent options:0 error:nil]; 
NSArray *XMLofPosts = [XMLdoc nodesForXPath:@"//methodResponse/params/param/value/array/data/value/struct/member" error:nil]; 

給了我所有的柱構件組成的數組,我可以通過
for(GDataXMLElement *postMember in XMLofPosts){ ... }

Thansk迭代的幫助。 我已經打開了關於這個問題的更詳細的問題,一個新的職位,因爲我發現這幾乎是一個不同的問題,因爲它是在GDataXML超過獲得WordPress的XML GDataXML nodesForXPath on Node contains items of entire GDataXMLDocument

0

我很高興與Wordpress JSON plugin,它是免費的。您可以輕鬆地將您的請求調整到特定的頁面和文章類型,這是有據可查的。從網站獲取的內容始終是JSON字典和數組的結構(比XML更好)。檢查JSON結構的好方法是JSON viewer

一些示例代碼開始。

- (void)loadNewsForPage:(NSUInteger)page 
{ 

    NSURLSession *session = [NSURLSession sharedSession]; 
    [[session dataTaskWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.mywordpresssite.com/api/get_posts/?page=%lu", (unsigned long)page]] 
      completionHandler:^(NSData *data, 
           NSURLResponse *response, 
           NSError *error) { 

       // handle response 
       NSError *jsonError = nil; 
       id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError]; 

       if (!jsonError) { 
        if ([jsonObject isKindOfClass:[NSDictionary class]]) { 

         // fill datastore 
         [self newsIntoDataStore:(NSDictionary *)jsonObject forPage:page]; 
        } 
        else { 
         NSLog(@"returned jsonObject is not a dictionary!"); 
        } 
       } else { 
        NSLog(@"jsonError, news: %@", jsonError); 
       } 

      }] resume]; 
} 

- (void)newsIntoDataStore:(NSDictionary *)news forPage:(NSUInteger)page 
{ 
    if ([[news objectForKey:@"status"] isEqualToString:@"ok"]) { 

     self.newsPages = [[news objectForKey:@"pages"] integerValue]; 
     NSArray *posts = [news objectForKey:@"posts"]; 

     if (posts.count > 0) { 
      // store individual posts 

      for (NSDictionary *post in posts) { 

       // determine post slug 
       NSArray *categoriesArray = [post objectForKey:@"categories"]; 
       NSString *postSlug = nil; 
       NSString *desiredSlug = @"news"; 

       if (categoriesArray.count > 0) { 
        for (NSDictionary *category in categoriesArray) { 
         if ([[category objectForKey:@"slug"] isEqualToString:desiredSlug]) { 
          postSlug = desiredSlug; 
         } 
        } 
       } 

        NSString *title = [post objectForKey:@"title"]; 

        NSDate *dateFromAPI = [self.dateFormatterFromAPI dateFromString:[post objectForKey:@"date"]]; 
        NSString *date = [self.dateFormatterForCell stringFromDate:dateFromAPI]; 

        NSString *imageURLStringOrg = [[[post objectForKey:@"thumbnail_images"] objectForKey:@"full"] objectForKey:@"url"]; 
        NSString *imageURLStringConverted = [imageURLStringOrg stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

        NSAttributedString *attributedContentString = [[NSAttributedString alloc] initWithData:[[post objectForKey:@"excerpt"] dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil]; 
        NSString *content = [attributedContentString string]; 

        NSString *url = [post objectForKey:@"url"]; 

        if (title && date && imageURLStringConverted && content && url) { 

         NSDictionary *postDictionary = @{@"title" : title, @"date" : date, @"imageURL" : imageURLStringConverted, @"content" : content, @"url" : url}; 

         [self.newsArray addObject:postDictionary]; 

        } else { 
         NSLog(@"some post item empty, skipping this post: %lu", (unsigned long)[posts indexOfObject:post]); 
        } 
       } 

      } 

      dispatch_async(dispatch_get_main_queue(), ^{ 

       if (self.pageLoaded < self.newsPages) { 
        [self loadNewsForPage:self.pageLoaded + 1]; 
       } else { 

        [self.delegate newsFetchingCompleted]; 
       } 
      }); 

     } else { 
      NSLog(@"no objects in the array"); 
     } 
    } else { 
     NSLog(@"returned status NOT OK"); 
    } 
} 
+0

onfortunately,這樣我可以不得到的menu_order,它刪除了在wordpress後端手動排序帖子的能力 似乎WPXMLRPCDecoder也返回json代碼,但它然後只保存爲一個對象(實際上是一個NSobject),有沒有辦法將這個成一個NSDictionary? –

+0

NSObject只是NSDictionary的超類。處理返回的對象是示例代碼的一部分,請參閱//處理響應。檢查返回的對象的種類。一旦你用NSDictionary成功了,你就開始解析它。 –

+0

它適用於您的代碼,如果我使用if對象iskindofclass nsdictionary從WPXMLRPCDecoder返回的對象它返回false,甚至tho該對象的NSLog輸出控制檯上看起來完全相似的代碼與JSON –