问题
I am trying to create an iOS app which (besides a few other things) needs to load in the content of a Wordpress-Page
When I use the getPosts feature from Wordpress's XML-RPC feature, I get the following returned:
<?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><!--:de-->Post1<!--:--><!--:en-->Post1<!--:--></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><!--:de--><b>Post1</b><!--:--><!--:en--><b>Post1</b><!--:--></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><!--:de-->Post2<!--:--><!--:en-->Post2<!--:--></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><!--:de--><b>Post2</b><!--:--><!--:en--><b>Post2</b><!--:--></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><!--:de-->Post3<!--:--><!--:en-->Post3<!--:--></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><!--:de--><b>Post3</b><!--:--><!--:en--><b>Post3</b><!--:--></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>
Now I'm trying to get a few fields I need (content, title, id, etc.) per post via the following two foreach-statements:
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];
// ...
}
}
However I'm facing the issue, that postMembers contains ALL members in the XML Document, not just those of one "post". Am I doing something wrong or is there a way to get around this?
回答1:
With any XPath query starting with /
, you tell the XPath engine to ignore the current context and start at the root instead. Using //value
, you're querying for all <value/>
elements from the root, no matter what context item you pass.
Using following XPath queries should return the elements you're looking for:
Finding the context. I removed the unnecessary second
/
in the beginning (which will slow down the evaluation of your query) and added the/struct
axis step (I guess you just forget that?)./methodResponse/params/param/value/array/data
Return all
<member/>
s for the current context:value/struct/member
Alternatively you could also explicitly denote using the current context
.
(which I prefer, as it clearly says what I wanted the XPath expression to do) using./value/struct/member
来源:https://stackoverflow.com/questions/22132037/gdataxml-nodesforxpath-on-node-contains-items-of-entire-gdataxmldocument