ApiXml.mm 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #import <Foundation/Foundation.h>
  2. #import "ApiXml.h"
  3. /*
  4. XML 解析库
  5. */
  6. @implementation XMLHelper
  7. -(void) startParse:(NSData *)data{
  8. dictionary =[NSMutableDictionary dictionary];
  9. contentString=[NSMutableString string];
  10. //Demo XML解析实例
  11. xmlElements = [[NSMutableArray alloc] init];
  12. xmlParser = [[NSXMLParser alloc] initWithData:data];
  13. [xmlParser setDelegate:self];
  14. [xmlParser parse];
  15. }
  16. -(NSMutableDictionary*) getDict{
  17. return dictionary;
  18. }
  19. //解析文档开始
  20. - (void)parserDidStartDocument:(NSXMLParser *)parser{
  21. //NSLog(@"解析文档开始");
  22. }
  23. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
  24. //NSLog(@"遇到启始标签:%@",elementName);
  25. }
  26. - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
  27. //NSLog(@"遇到内容:%@",string);
  28. [contentString setString:string];
  29. }
  30. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
  31. //NSLog(@"遇到结束标签:%@",elementName);
  32. if( ![contentString isEqualToString:@"\n"] && ![elementName isEqualToString:@"root"]){
  33. [dictionary setObject: [contentString copy] forKey:elementName];
  34. //NSLog(@"%@=%@",elementName, contentString);
  35. }
  36. }
  37. //解析文档结束
  38. - (void)parserDidEndDocument:(NSXMLParser *)parser{
  39. //NSLog(@"文档解析结束");
  40. //[xmlElements release];
  41. //[xmlParser release];
  42. }
  43. @end