gpt4 book ai didi

ios - 无法使用 NSXmlParser 获取数据

转载 作者:行者123 更新时间:2023-11-29 03:18:17 24 4
gpt4 key购买 nike

我正在使用 NSXmlParserXML 获取数据。在我的解析器委托(delegate)方法中:

- (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 中获取“test”和其他值。

最佳答案

为什么使用可变数组。简单地试试这个

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 

if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];

NSLog(@"Processing Value: %@", currentElementValue);

}

关于ios - 无法使用 NSXmlParser 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21422971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com