ios - 密码的 XML 解析,无需在数据库中存储/缓存

标签 ios xml xml-parsing

我遇到的情况是,我返回了一个非常简单的 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/

相关文章:

ios - Xcode 7 测试版警告 : Interface Orientations and Launch Storyboard

ios - Xcode 7.3 的 WCSession 问题

ios - 在调用 webservice 之前检查 internet 是 On 还是 Off

java - 在 Java 中访问网络,命令行参数?

java - 简单 XML 序列化第三方库

c# - 解析格式错误的 XML

ios - Swift iOS - 游戏重置时角色动画静态

php - 使用 PHP 生成 XML 文件

javascript - 如何在正文中制作标签以正确显示

java - 在 Jaxb 解码时希望未知属性出错