开发者

constructing a string from a file in ios

I am trying to read a file which contains something below information

<TEXTFORMAT LEADING='2'><P ALIGN='LEFT'><FONT FACE='Arial' SIZE='10' COLOR='#000000' LETTERSPACING='0' KERNING='0'>Kiran</FONT></P></TEXTFORMAT> <TEXTFORMAT LEADING='2'><P ALIGN='LEFT'><FONT FACE='Arial' SIZE='10' COLOR='#000000' LETTERSPACING='0' KERNING='0'> Ajay</FONT></P></TEXTFORMAT>&l开发者_StackOverflow中文版t;/isText><isValue></isValue><isCorrectAnswer></isCorrectAnswer>

in that i need a string that is start with <p and ends with /p> .what is the best possible way.

**My Idea 1.Reading a character by character and checking

2.using NSXmlParser to parse the above

Please help me out which is the good one and give your suggestion other than this approach if you have any good technique.

Your suggestion more value for me. Thanks All.


You can try using TouchXML: TouchXML installation

and here is the code that solves your problem:

NSMutableArray *res = [[NSMutableArray alloc] init];
//xmlInput is your string as NSString
CXMLDocument *doc = [[[CXMLDocument alloc] initWithData:[xmlInput dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil] autorelease];

NSArray *nodes = NULL;
nodes = [doc nodesForXPath:@"////P" error:nil];

for (CXMLElement *node in nodes) {
    NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
    [item setObject:[node XMLString] forKey:@"p"];        
    [res addObject:item];
    [item release];
}
NSLog(@"%@", res);
[res release];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜