2

I have an example XML like this:

<rss version="2.0"> 
<channel>
     <title></title>
     <link></link>
     <description></description>
<item>
   <title></title>
   <link></link>
   <description></description>
   <pubDate></pubDate>
   <guid></guid>
</item>

and so on… And my obj-c syntax is looking like this:

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

DDXMLDocument *ddDoc = [[DDXMLDocument alloc] initWithData:data options:0 error:&error];

NSArray *xmlItems = [ddDoc nodesForXPath:@"//item" error:&error]; // where ddDoc is your DDXMLDocument
NSMutableArray *returnArray = [[NSMutableArray alloc] initWithCapacity:[xmlItems count]];

for(DDXMLElement* itemElement in xmlItems)
{
    ActiveActivity *activity = [[ActiveActivity alloc] init];

    NSString *itemValueAsString = [[itemElement attributeForName:@"link"]stringValue];


    [returnArray addObject:activity];
}

It's returning no value, can anyone say me why?

btype
  • 1,593
  • 19
  • 32

3 Answers3

4

The problem is in this line:

 NSString *itemValueAsString = [[itemElement attributeForName:@"link"]stringValue];

link is not an attribute, but rather an element. So try this:

 NSString *itemValueAsString = [[[itemElement elementsForName:@"link"] lastObject] stringValue];
Daniel Rinser
  • 8,855
  • 4
  • 41
  • 39
0

For Swift 3,

let object: DDXMLElement = element.elements(forName: "objectKey").first!
yourModelClass.stringVariable = object.stringValue!
Pankaj Gaikar
  • 2,357
  • 1
  • 23
  • 29
-1
NSArray *xmlItems = [[ddDoc rootElement] elementsForName:@"item"];
NeverBe
  • 5,213
  • 2
  • 25
  • 39