Alright, your first step would be to make a class that will parse the data. Call it RecordParser
, for example. We now need to add a couple methods in the header, as well as a NSMutableArray
.
@interface RecordParser : NSObject {
NSMutableArray *records;
}
@property(nonatomic,retain)NSMutableArray *records;
-(void)loadRecords:(NSString *)records;
-(void)traverseElement:(TBXMLElement *)element;
@end
Now, go ahead and charge into your implementation. We now need to implement those two methods to do what we want them to do.
- (void)loadRecords:(NSString *)records {
NSString *someXML = @"http://www.something.com/somexml.xml";
TBXML *tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:someXML]] retain];
records = [NSMutableArray array];
[records retain];
if (tbxml.rootXMLElement)
[self traverseElement:tbxml.rootXMLElement];
[tbxml release];
}
Basically that method will grab the XML file in question and begin the parsing process. Also, you're initializing your array and retaining it. Now we come to the cheese.
- (void) traverseElement:(TBXMLElement *)element {
do {
if (element->firstChild)
[self traverseElement:element->firstChild];
if ([[TBXML elementName:element] isEqualToString:@"Record"]) {
TBXMLElement *destination = [TBXML childElementNamed:@"Destination" parentElement:element];
TBXMLElement *status = [TBXML childElementNamed:@"Status" parentElement:element];
TBXMLElement *guid = [TBXML childElementNamed:@"GUID" parentElement:element];
TBXMLElement *dateSub = [TBXML childElementNamed:@"DateSubmitted" parentElement:element];
TBXMLElement *dateToSend = [TBXML childElementNamed:@"DateToSend" parentElement:element];
TBXMLElement *dateSent = [TBXML childElementNamed:@"DateSent" parentElement:element];
TBXMLElement *dateReceived = [TBXML childElementNamed:@"DateReceived" parentElement:element];
TBXMLElement *message = [TBXML childElementNamed:@"Message" parentElement:element];
TBXMLElement *id = [TBXML childElementNamed:@"ID" parentElement:element];
[records addObject:[NSArray arrayWithObjects:
[TBXML textForElement:destination],
[TBXML textForElement:status],
[TBXML textForElement:guid],
[TBXML textForElement:dateSub],
[TBXML textForElement:dateToSend],
[TBXML textForElement:dateSent],
[TBXML textForElement:dateReceived],
[TBXML textForElement:message],
[TBXML textForElement:id],nil]];
}
} while ((element = element->nextSibling));
}
Basically what the method does is transverse the XML file looking for an element with the name you're looking for, then it grabs the data from the child nodes. Additionally, the data is added to the records
array. So basically, when it's done it should have the data you're wanting in your records
array, which you can manipulate all you want.
This is completely untested. Don't blame me if it blows up your computer and kills your cat. I normally wouldn't take all the work to write a complete method like this, but I happen to like TBXML
. Please let me know if it works. I really would appreciate knowing.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…