我正在使用NSXmlParser
从XML
获取数据。在解析器委托方法中,我有:
- (void)resetPassCallback: (NSData*) data{
NSLog(@"start parsing the data");
NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithData:data];
[nsXmlParser setDelegate:self];
BOOL success = [nsXmlParser parse];
if (success) {
NSLog(@"No errors %@", xmlArray);
} else {
NSLog(@"Error parsing document!");
}
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qualifiedName attributes: (NSDictionary *)attributeDict
{
element = elementName;
if([elementName isEqualToString:@"User"])
{
XmlDict = [[NSMutableDictionary alloc]init];
userId =[[NSMutableString alloc]init];
screenName =[[NSMutableString alloc]init];
xmlArray = [[NSMutableArray alloc]init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
NSLog(@"foundCharacters ::::: %@ :::: ", string);
[xmlArray addObject:string];
}
虽然我能够解析数据,但我没有得到完整的数据。在控制台中,日志显示:
foundCharacters ::::: 200 ::::
foundCharacters ::::: f1f47453-04d7-20c8-a9e1-406fdc89a2da ::::
foundCharacters ::::: test ::::
foundCharacters ::::: 62e2092a-eb6e-44ad-7a7b-3deb976569c1 ::::
foundCharacters ::::: Thomas ::::
xmlArray contents : (
"62e2092a-eb6e-44ad-7a7b-3deb976569c1",
Thomas
)
正如您所看到的,为什么我没有在xmlArray
中获得“测试”和其他值。
答案 0 :(得分:1)
为什么要用于mutableArray。试试这个
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(@"Processing Value: %@", currentElementValue);
}
答案 1 :(得分:0)
我希望,每次调用NSXMLParser
个代表时,Array都会重新初始化。
您需要在
NSXMLParser delegates
之前初始化数组 调用。
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qualifiedName attributes: (NSDictionary *)attributeDict
{
//Don't initialise your NSArray here
}
}
-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
NSLog(@"foundCharacters ::::: %@ :::: ", string);
[xmlArray addObject:string];
}
答案 2 :(得分:0)
在
中使用xmlArray = [[NSMutableArray alloc]init];
- (void)resetPassCallback: (NSData*) data{
NSLog(@"start parsing the data");
xmlArray = [[NSMutableArray alloc]init];
NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithData:data];
[nsXmlParser setDelegate:self];
BOOL success = [nsXmlParser parse];
if (success) {
NSLog(@"No errors %@", xmlArray);
} else {
NSLog(@"Error parsing document!");
}
}
不在
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qualifiedName attributes: (NSDictionary *)attributeDict
{
}