據我所見,它工作得很好。
@implementation XMLDELEGATE
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
NSLog(@"didStartElement(%@)",elementName);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"didEndElement(%@)",elementName);
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSLog(@"foundCharacters(%@)",string);
}
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
static const unsigned char bytes[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Details>S |nºconta|</Details>";
NSXMLParser *p;
NSData *d = [NSData dataWithBytes:bytes length:sizeof(bytes)-1];
p = [[NSXMLParser alloc] initWithData:d];
NSLog(@"data=%@",d);
p.delegate = [XMLDELEGATE new];
[p parse];
}
@end
輸出這樣的:
2014-03-01 15:35:07.272 xmlp2[35923:303] data=<3c3f786d 6c207665 7273696f 6e3d2231 2e302220 656e636f 64696e67 3d225554 462d3822 3f3e3c44 65746169 6c733e53 207c6ec2 ba636f6e 74617c3c 2f446574 61696c73 3e>
2014-03-01 15:35:07.273 xmlp2[35923:303] didStartElement(Details)
2014-03-01 15:35:07.273 xmlp2[35923:303] foundCharacters(S |n)
2014-03-01 15:35:07.273 xmlp2[35923:303] foundCharacters(ºconta|)
2014-03-01 15:35:07.273 xmlp2[35923:303] didEndElement(Details)
正如我在對方的回答說,你可能不重視的多次調用foundCharacters。作爲代表,您有責任將多個字符串數據塊粘貼在每個元素中(如果發生這種情況,則爲!CDATA)
[未從xml解析中獲取正確的字符串](http:// stackoverflow。 com/questions/21950788/not-getting-proper-string-from-xml-parsing) – CRD