开发者

how to read attributes of an element in xml file using objective c?

In my application am getting an xml file from server and in that i have some attributes for the element.i donno how to read the attributes .The following is my XML file

 <History> 
    <Result ImageId="4507" Description="" Date="10/19/2010 12:49:22 AM" />
    <Result ImageId="4505" Description="" Date="10/18/2010 8:01:28 AM" />
 </History>

so i want to assign ImageId,Description and Date into three variabl开发者_高级运维es and i want to save each result into an array. Reading data from that array.Please help me..


Standard iPhone way

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/XMLParsing/Articles/UsingParser.html

Handling XML Elements and Attributes

another way to parse Using libxml2 for XML parsing and XPath queries in Cocoa

I know there are third party libraries that simplify the process


assuming that you are going to store the attribute values into NSStrings.

NSString *ImageId;
NSString *Description;
NSString *Date;

you can use simple if statements.
if([Elementname isEqualToString:@"Result"]){
  ImageId = [attributeDict objectForKey:@"ImageId"];
  Description = [attributeDict objectForKey:@"Description"];
  Date = [attributeDict objectForKey:@"Date"];
}

So as you can see first you have to decide which element you are interested in which your case would be the "Result" element. Then you are telling the compiler that if the elementname is "Result" you want its attribute values stores under ImageId, Description & Date to be saved into NSStrings.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜