我使用XML解析器来获取数据。
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ( [elementName isEqualToString:@"country"] )
{
x++;
[messages addObject:[NSDictionary dictionaryWithObjectsAndKeys:Id,@"ID",Name,@"NAME",Image,@"IMAGE",nil]];
//**111**
NSLog(@"Data in Message : %@", [messages objectAtIndex:(x-1)]);
[Id setString:@""];
[Name setString:@""];
[Image setString:@""];
}
if ( [elementName isEqualToString:@"id"] )
{
[Id appendString:currentNodeContent];
}
if ( [elementName isEqualToString:@"name"] )
{
[Name appendString:currentNodeContent];
}
if ( [elementName isEqualToString:@"im"] )
{
[Image appendString:currentNodeContent];
}
}
//在// * 111 * 消息被打印但是当我在调用xml解析器后执行此操作时,它不会打印:
chatParser = [[NSXMLParser alloc] initWithData:receivedData];
[chatParser setDelegate:self];
[chatParser parse];
NSLog(@"\n\nMessages...\n\n%@", messages); //here there is no data printing
此时打印空了..
消息。
(
{
ID = "";
IMAGE = "";
NAME = "";
},
{
ID = "";
IMAGE = "";
NAME = "";
},
{
ID = "";
IMAGE = "";
NAME = "";
},
{
ID = "";
IMAGE = "";
NAME = "";
},
{
ID = "";
IMAGE = "";
NAME = "";
},
)
我已定义消息属性并将其合并。 在 viewDidLoad 中,我做了这个
messages=[[NSMutableArray alloc] init];
感谢您的帮助..
答案 0 :(得分:0)
使用Below Code.Where Table是NSObject类,CaseId,PartyId,CartId作为此类中的属性。如果你有xl url刚刚用URl调用loadXMLByURL方法。解析之后你会得到TableArray中的每个Object拥有具有上述属性的Table对象。
NSMutableString *currentNodeContent;
NSXMLParser *parser;
Tweet *currentTweet;
bool isStatus;
-(id) loadXMLByURL:(NSString *)urlString
{
_tweets = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];
return self;
}
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementname isEqualToString:@"Table"])
{
currentTable = [Table alloc];
isStatus = YES;
}
}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if (isStatus)
{
if ([elementname isEqualToString:@"CaseId"])
{
currentTable.CaseId = currentNodeContent;
}
if ([elementname isEqualToString:@"PartyId"])
{
currentTable.PartyId = currentNodeContent;
}
if ([elementname isEqualToString:@"CartId"])
{
currentTable.CartId = currentNodeContent;
}
}
if ([elementname isEqualToString:@"Table"])
{
[self.tableArray addObject:currentTable];
currentTable = nil;
currentNodeContent = nil;
}
}
如果您有任何疑问,请告诉我。
答案 1 :(得分:0)
您的问题是您始终使用相同的对象Id, Name, Image
并使用
[Id setString:@""];
[Name setString:@""];
[Image setString:@""];
在每个国家/地区元素的末尾将其值设置为“”。
最好看起来像这样(没有经过测试,可能是一些错别字):
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"id"] )
{
[lastMessage setObject:currentNodeContent forKey:"ID"];
}
else if ( [elementName isEqualToString:@"name"] )
{
[lastMessage setObject:currentNodeContent forKey:"NAME"];
}
else if ( [elementName isEqualToString:@"im"] )
{
[lastMessage setObject:currentNodeContent forKey:"IMAGE"];
}
else if ( [elementName isEqualToString:@"country"] )
{
NSLog(@"Data in Message : %@", lastObject);
[messages addObject:lastMessage];
[lastMessage release];
}
}
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elName namespaceURI:(NSString *)uri
{
if ( [elementName isEqualToString:@"country"] )
{
lastMessage = [[NSMutableDictionary alloc]init];
}
}