English 中文(简体)
1. 蒸汽
原标题:iPhone xml deserializer

I am new to programming and this is my first project so I don t have much idea about xml parsing etc. I need to parse an xml document like...

<TLMS><TLMSResponse status="SUCCESS">
 <books>
  <book>
   <name>abc</name>
   <author>DEF</author>
   <publisher>HIJ</publisher>
  </book>

  <book>
   <name>xyz</name>
   <author>rus</author>
   <publisher>tuv</publisher>
  </book>
 </books>
</TLMSResponse></TLMS>

I have tried a lot but haven t found any good de-serializer that can parse this xml and make objects of "Book" class. Any help is greatly appreciated...

感谢大家

最佳回答

The following s a example section implementing the NSXParserDelegate。

It would parse this xml:

<TLMS><TLMSResponse status="SUCCESS">
<books>
<book>
<name>abc</name>
<author>DEF</author>
<publisher>HIJ</publisher>
</book>

<book>
<name>xyz</name>
<author>rus</author>
<publisher>tuv</publisher>
</book>
</books>
</TLMSResponse></TLMS>

每个字典都是Xml的“书”,每个字体的钥匙和作为钥匙的标签之间的文字。 因此,returnData in [_delegateceptParsedData:[returnData autorelease] withIdent:ident 希望:

array = 
(
    {
        name:"abc",
        author:"DEF",
        publisher:"HIJ",
    },
    {
        name:"xyz",
        author:"rus",
        publisher:"tuv",
    },
)

JHXMLParser.h

@protocol JHXMLParserDelegate;

@interface JHXMLParser : NSObject <NSXMLParserDelegate> {
    NSString *ident;

    @private
    id _delegate;
    NSMutableArray *_parsedData;
    NSString *_key;
    NSXMLParser *_dataParser;
    NSString *_previousTag;
    NSString *_currentTag;
    NSMutableString *_elementText;
}

@property (nonatomic, retain) NSString *ident;

- (id)initWithKey:(NSString *)Key andData:(NSData *)data;
// delegate management. The delegate is not retained.
- (id <JHXMLParserDelegate>)delegate;
- (void)setDelegate:(id <JHXMLParserDelegate>)delegate;
- (BOOL)start;

@end

@protocol JHXMLParserDelegate <NSObject>

@optional
- (void)acceptParsedData:(NSMutableArray *)parsedData withIdent:(NSString *)ident;

@end

JHXMLParser.m

#import "JHXMLParser.h"

@implementation JHXMLParser

@synthesize ident;

- (id)init {
    if ((self = [super init])) {
        ident = [[NSString alloc] init];
    }
    return self;
}

- (id)initWithKey:(NSString *)key andData:(NSData *)data {
    if ((self = [self init])) {
        _key = key;
        _elementText = [[NSMutableString alloc] initWithString:@""];
        _dataParser = [[NSXMLParser alloc] initWithData:data];
        _dataParser.delegate = self;
    }
    return self;
}

- (id <JHXMLParserDelegate>)delegate {
    id <JHXMLParserDelegate> d = nil;
    if (_delegate) {
        d = _delegate;
    }
    return d;
}
- (void)setDelegate:(id <JHXMLParserDelegate>)delegate {
    _delegate = delegate;
}

- (BOOL)start {
    return [_dataParser parse];
}

- (void)dealloc {
    [ident release];
    [_dataParser release];
    [_parsedData release];
    [_elementText release];
    [_previousTag release];
    [_currentTag release];
    [super dealloc];
}

#pragma mark - NSXMLParser Delegate

- (void)parserDidStartDocument:(NSXMLParser *)parser {
    _parsedData = [[NSMutableArray alloc] init];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
    if (_currentTag) {
        [_currentTag release], _currentTag = nil;
    }
    _currentTag = [[NSString alloc] initWithString:elementName];
    if ([elementName isEqualToString:_key]) {
        NSMutableDictionary *tmpDict = [[NSMutableDictionary alloc] init];
        [_parsedData addObject:tmpDict];
        [tmpDict release];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    if ([_previousTag isEqualToString:_currentTag]) {
        [_elementText appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
    } else {
        if (_elementText) {
            [_elementText release], _elementText = nil;
        }
        _elementText = [[NSMutableString alloc] initWithString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
    }
    if (![_previousTag isEqualToString:_currentTag]) {
        if (_previousTag) {
            [_previousTag release], _previousTag = nil;
        }
        _previousTag = [[NSString alloc] initWithString:_currentTag];
    }
    [pool drain];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    if ([_previousTag isEqualToString:elementName]) {
        [[_parsedData lastObject] setObject:_elementText forKey:elementName];
    } else {
        if (_previousTag) {
            [_previousTag release], _previousTag = nil;
        }
        _previousTag = [[NSString alloc] initWithFormat:@""];
    }
    [pool drain];
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
    NSMutableArray *returnData = [[NSMutableArray alloc] initWithArray:_parsedData];
    if ([_delegate respondsToSelector:@selector(acceptParsedData:withIdent:)]) {
        [_delegate acceptParsedData:[returnData autorelease] withIdent:ident];
    } else {
        [returnData release], returnData = nil;
    }
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    NSLog(@"********** ERROR Parsing (%@) XML Data! - %@ - %@ **********", ident, [parseError localizedDescription], [parseError localizedFailureReason]);
    if ([_delegate respondsToSelector:@selector(acceptParsedData:withIdent:)]) {
        [_delegate acceptParsedData:_parsedData withIdent:ident];
    }
}

@end
问题回答

暂无回答




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签