我遇到的情况是,我返回了一个非常简单的 XML 正文,但我想将其解析为字符串,而不会将其意外存储在任何地方。由于这是一个如此简单的案例,我想知道解析这个问题的最佳方法是什么?
<user>
<password> holla </password>
</user>
感谢您的宝贵时间!
最佳答案
您需要有一个 NSURLConnection 对象和一个 NSXMLParser 对象。我相信您已经知道了。
假设你在某处有一个 NSString *tempString 。
对于 NSXMLParser,以下是您必须实现的方法:
// When the start of an element is found
- (void) parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"password"])
{
// initialize your string
tempString = [NSString alloc] init];
}
}
// When the text in an element is found
- (void) parser:(NSXMLParser *) parser
foundCharacters:(NSString *)string
{
// use the value of the string(password) to initialize your string
tempString = string;
}
// When the end of element is found
- (void) parser:(NSXMLParser *) parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
// whatever work left to do with the xml parsing
// use know you get the password string, so do whatever you want after that
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Got the password!"
message:@""
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
关于ios - 密码的 XML 解析,无需在数据库中存储/缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9404192/