Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用正则表达式进行Xml解析时出现问题_Iphone_Objective C_Regex_Xml Parsing - Fatal编程技术网

Iphone 使用正则表达式进行Xml解析时出现问题

Iphone 使用正则表达式进行Xml解析时出现问题,iphone,objective-c,regex,xml-parsing,Iphone,Objective C,Regex,Xml Parsing,这是我通过webservice收到的xml <?xml version="1.0" encoding="utf-8"?> <Quotes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://swanandmokashi.com"> <QuoteOfTheDay>Hit any user

这是我通过webservice收到的xml

<?xml version="1.0" encoding="utf-8"?>
<Quotes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://swanandmokashi.com">
  <QuoteOfTheDay>Hit any user to continue.</QuoteOfTheDay>
  <Author>Fuzzel Fish Administration Page</Author>
</Quotes>

点击任何用户继续。
Fuzzel鱼管理页面
我试图在我的ConnectiondFinishLoading中通过以下方式解析它:

{            
 webData_str=[[NSString alloc]initWithData:webData encoding:NSUTF8StringEncoding];
 NSString *pattern = @"<QuoteOfTheDay>(.*)</QuoteOfTheDay><Author>(.*)</Author>";
 NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
                        options:NSRegularExpressionCaseInsensitive                                                                            error:nil];

__block NSString *QuoteString,*author= nil;

[regex enumerateMatchesInString:webData_str options:0 range:NSMakeRange(0, [webData_str length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {

   // not executing...

    if (0 < [match numberOfRanges]) {
        NSRange range = [match rangeAtIndex:1];
        QuoteString = [webData_str substringWithRange:range];


        NSRange range1 = [match rangeAtIndex:2];
        author = [webData_str substringWithRange:range1];        

    }
}];        

final=[NSString stringWithFormat:@"\n%@\n%@",QuoteString,author];
NSLog(@"Final--- %@",final);
}
{
webData_str=[[NSString alloc]initWithData:webData编码:NSUTF8StringEncoding];
NSString*模式=@“(.*)(.*)”;
NSRegularExpression*regex=[NSRegularExpression regular expressionwithpattern:pattern
选项:nsregularexpressioncase不敏感错误:nil];
__block NSString*QuoteString,*author=nil;
[regex EnumerateMatchesInstalling:webData\u str选项:0范围:NSMakerRange(0,[webData\u str length])使用块:^(NSTextCheckInResult*匹配,NSMatchingFlags标志,BOOL*停止){
//不执行。。。
如果(0<[匹配数量范围]){
NSRange range=[匹配范围索引:1];
QuoteString=[webData_str substringWithRange:range];
NSRange range 1=[匹配范围索引:2];
作者=[webData_str substringWithRange:range1];
}
}];        
最终=[NSString stringWithFormat:@“\n%@\n%@”,引号,作者];
NSLog(@“最终-%@”,最终);
}

这是怎么回事,执行流不在块内部。

更好的解决方案是使用XML解析器(例如,
NSXMLParser
)而不是常规的XML解析器 表情

但要回答您的具体问题:您的正则表达式与
之间的空格(换行符和空格)不匹配。如果您将模式更改为

NSString *pattern = @"<QuoteOfTheDay>(.*)</QuoteOfTheDay>\\s*<Author>(.*)</Author>";
NSString*模式=@“(.*)\\s*(.*)”;

您将获得预期的输出。

太好了。谢谢。。是的,NSXMlParser是更好的选择,但由于标记的数量只有两个,所以我选择正则表达式。。