如何在iPhone中将XML读取为XML
我有一个自定义的WordPress的post,看起来像这样,当查看源代码。
我怎样才能读取它parsing为XML?
<title>A sample quiz</title> <content>Hello.</content> <updatedDate>18/01/12</updatedDate> <updatedTime>16:11</updatedTime> <question>How many days are there in a week?</question> <hint>No hint</hint> <direction>Left</direction> <answer1>4</answer1> <answer2>5</answer2> <answer3>6</answer3> <answer4>7</answer4> <correctAnswer>4</correctAnswer> <score>20</score>
记得在你的h文件中设置。 NSXMLParserDelegate并设置
- (void)didload { NSString *site = [NSString stringWithFormat:@"http://www.mysite.it/mydirectory/myxml"]; NSURL *xmlURL = [NSURL URLWithString:site]; myParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL]; [myParser setDelegate:self]; [myParser parse]; } - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict { if ([elementName isEqualToString:@"title"]) { NSLog(@"element %@",attributeDict); //do something } else if ([elementName isEqualToString:@"content"]){ NSLog(@"element %@",attributeDict); //do something } //ecc }
这是非常简化的方法。 更多详细信息http://www.xcode-tutorials.com/parsing-xml-files/