NSXMLParser错误代码4(文档为空)和(解析器:didStartElement:namespaceURI:qualifiedName:attributes :)未被调用

时间:2014-12-17 10:52:21

标签: ios xml parsing

我正在尝试从web api解析xml文件中的Projects信息列表。文件的开头如下:

<ArrayOfProject xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/">
<Project>
 <ExpectedEnergizingDate>2016-10-30T00:00:00</ExpectedEnergizingDate>
 <PlannedDate>2017-04-02T00:00:00</PlannedDate>
 <ProjectID>1-1432063</ProjectID>
 <ProjectName>
  Construction of Industrial7 SS in Jeddah and Connection With Network
 </ProjectName>
</Project>
<Project>
 <ExpectedEnergizingDate>2017-03-13T00:00:00</ExpectedEnergizingDate>
 <PlannedDate>2017-04-01T00:00:00</PlannedDate>
 <ProjectID>1-1432073</ProjectID>
 <ProjectName>
  Construction of Haram Road SS in Makkah Almukrmah and Connection With Network
 </ProjectName>
</Project>

这些是我的initWithData和解析器委托的主要方法:

- (id)initWithData:(NSData *)parseData
{
    if (self = [super init])
    {
        projectsData = [parseData copy];

        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
        [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
        [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];

    }
    return self;
}

// the main function for this NSOperation, to start the parsing
- (void)main
{
    self.currentParseBatch = [NSMutableArray array];
    self.currentParsedCharacterData = [NSMutableString string];

    //Printing the response
    NSString *strData = [[NSString alloc]initWithData:self.projectsData encoding:NSUTF8StringEncoding];
    NSLog(@"%@",strData);
    //END

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:self.projectsData];

    [parser setDelegate:self];
    [parser parse];

    self.currentParseBatch = nil;
    self.currentProjectObject = nil;
    self.currentParsedCharacterData = nil;
}

问题是:虽然我确定收到了回复并且self.projectsData包含了我可以NSLog的信息,但解析器却提供了错误代码4 Document is empty,甚至没有调用方法parser: didStartElement: namespaceURI: qualifiedName: attributes:

1 个答案:

答案 0 :(得分:0)

我发现他的问题是http请求响应不是XML而是JSON,尽管浏览器将其显示为XML。