2014-03-02 76 views
1

我想創建一個iOS應用其中(除其他一些東西)需要當我使用getPosts從功能的WordPress的頁GDataXML nodesForXPath在節點包含整個GDataXMLDocument

的內容加載的項目WordPress的的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> 

現在我想通過以下兩種的foreach語句,讓每一個崗位幾個字段我需要(內容,標題,ID等) :

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

for(GDataXMLNode *post in XMLofPosts){ 
    NSArray *postMembers = [post nodesForXPath:@"//value/struct/member" error:nil]; 
    for(GDataXMLElement *postMember in postMembers){ 
     NSString *key = [[[postMember elementsForName:@"name"] objectAtIndex:0] stringValue]; 
     NSString *value = [[[postMember elementsForName:@"value"] objectAtIndex:0] stringValue]; 
     // ... 
    } 
} 

但是我面臨的問題是,postMembers包含XML文檔中的所有成員,而不僅僅是一個「帖子」。 我做錯了什麼或有辦法解決這個問題?

回答

0

對於以/開頭的任何XPath查詢,您告訴XPath引擎忽略當前上下文,而是從根處開始。使用//value,無論您傳遞什麼上下文項目,您都在查詢所有來自根目錄<value/>元素。

使用以下XPath查詢應該返回你正在尋找的元素:

  1. 尋找上下文。我在開始時刪除了不必要的第二個/(這會減慢對查詢的評估),並添加了/struct軸步(我想你只是忘記了這一點?)。

    /methodResponse/params/param/value/array/data 
    
  2. 返回所有<member/> S爲當前環境:

    value/struct/member 
    

    另外,您也可以明確地表示使用當前上下文.(我更喜歡,因爲它清楚地說,我想要的東西XPath表達式使用

    ./value/struct/member 
    
+0

做),我不知道爲什麼我不能讓它工作。 的XML存在的結構: '' 予先用 '/ methodResponse/PARAMS/PARAM /值/陣列/數據/ value' 然後在第二foreach我繼續 '。/ value/struct/member' while the first statement works fine,if I do 'NSArray * postMembers = [post nodesForXPath:@「./value/struct/member」error:nil] ; NSLog(@「postmembers:%@」,postMembers); ' 輸出爲空 –

+0

噢,好吧,我發現第一個我使用的問題 :'/ methodResponse/params/param/value/array/data/value' 和第二我使用'。 /結構/成員「(該點已經指的是最重要的項目,所以在這種情況下,它代表」價值「) 非常感謝!我很感謝你的快速幫助 –

+0

對不起,只是意識到這是我的不好。感謝你整理出來,會更新答案。 –