这是我的教导部分,在这里,我正在从Xml档案中获取属性和价值。 现在,Xml将同时 par平时,表层的特性和价值就应逐字显示。
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(@"found this element: %@", elementName);
currentElement = [elementName copy];
if([elementName isEqualToString:@"ProductData"])
{
objectsArray = [[NSMutableArray alloc] init];
productDict = [[NSMutableDictionary alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(@"found characters: %@ %@", currentElement,string);
if(!currentString){
currentString = [[NSMutableString alloc] init];
}
[currentString appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName{
//NSLog(@"ended element: %@", elementName);
if([elementName isEqualToString:@"id"])
{
[productDict setObject:currentString forKey:@"id"];
[self labelsetting:0];
[currentString release],currentString = nil;
return;
}
if([elementName isEqualToString:@"productNumber"])
{
[productDict setObject:currentString forKey:@"productNumber"];
[self labelsetting:0];
[currentString release],currentString = nil;
return;
}
if([elementName isEqualToString:@"name"])
{
[productDict setObject:currentString forKey:@"name"];
[self labelsetting:0];
[currentString release],currentString = nil;
return;
}
if([elementName isEqualToString:@"dateCreated"])
{
[productDict setObject:currentString forKey:@"dateCreated"];
[self labelsetting:0];
[currentString release],currentString = nil;
return;
}
if([elementName isEqualToString:@"image"])
{
[productDict setObject:currentString forKey:@"image"];
[self labelsetting:0];
[currentString release],currentString = nil;
return;
}
if([elementName isEqualToString:@"ProductData"])
{
[objectsArray addObject:productDict];
[productDict release],productDict = nil;
}
[currentString release], currentString = nil;
}
下表是我想要显示xml数据的情况。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [objectsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15.0];
}
productDict = [objectsArray objectAtIndex:indexPath.row];
NSString *data = nil;
for (NSString *key in [productDict allKeys] ) {
// [data stringByAppendingFormat:@"%@ %@",key,[productDict objectForKey:key]];
data = [NSString stringWithFormat:@"%@%@", key,[productDict objectForKey:key]];
NSLog(@"data: %@",data);
}
cell.textLabel.text = data;
return cell;
[data release];
}
This the console of this view controller
2011-10-16 15:19:01.627 cmsCommander[1795:207] namestring: emon
2011-10-16 15:19:01.630 cmsCommander[1795:207] username: emon@test.com
2011-10-16 15:19:01.874 cmsCommander[1795:207] found file and started parsing
2011-10-16 15:19:01.877 cmsCommander[1795:207] current Element id : 1
2011-10-16 15:19:01.879 cmsCommander[1795:207] current Element productNumber : a91cc0f4c7
2011-10-16 15:19:01.880 cmsCommander[1795:207] current Element name : Product 1
2011-10-16 15:19:01.881 cmsCommander[1795:207] current Element image : 5e928bbae358c93caedf6115fa7d178b.jpg
2011-10-16 15:19:01.882 cmsCommander[1795:207] current Element dateCreated : 2011-10-06T16:08:45
2011-10-16 15:19:01.883 cmsCommander[1795:207] current Element id : 2
2011-10-16 15:19:01.888 cmsCommander[1795:207] current Element productNumber : d8287e2e51
2011-10-16 15:19:01.892 cmsCommander[1795:207] current Element name : Product 2
2011-10-16 15:19:01.893 cmsCommander[1795:207] current Element image : 8bbd8dfff3cdd28285d07810a4fe7c32.jpg
2011-10-16 15:19:01.895 cmsCommander[1795:207] current Element dateCreated : 2011-10-06T16:08:45
2011-10-16 15:19:01.896 cmsCommander[1795:207] current Element id : 3
2011-10-16 15:19:01.897 cmsCommander[1795:207] current Element productNumber : 7931c08c22
2011-10-16 15:19:01.897 cmsCommander[1795:207] current Element name : Product 3
2011-10-16 15:19:01.902 cmsCommander[1795:207] current Element image : e19becad20d6f4378e37313c5dbdf070.jpg
2011-10-16 15:19:01.904 cmsCommander[1795:207] current Element dateCreated : 2011-10-06T16:08:45
2011-10-16 15:19:01.904 cmsCommander[1795:207] all done!
2011-10-16 15:19:01.906 cmsCommander[1795:207] The number of row in the object array: 1
2011-10-16 15:19:01.909 cmsCommander[1795:207] data: name Product 3
2011-10-16 15:19:01.910 cmsCommander[1795:207] data: id 3
2011-10-16 15:19:01.911 cmsCommander[1795:207] data: productNumber 7931c08c22
2011-10-16 15:19:01.912 cmsCommander[1795:207] data: image e19becad20d6f4378e37313c5dbdf070.jpg
2011-10-16 15:19:01.913 cmsCommander[1795:207] data: **dateCreated 2011-10-06T16:08:45**
现在,我认为你已经提出问题。