Ome*_*han 4 iphone xcode objective-c ios xcode4.2
这是XML数据:
<Categorys>
<Category>
<categoryId>25</categoryId>
<categoryName>My Photos</categoryName>
<categoryDescription>Description</categoryDescription>
<categoryIconPath>7.jpg</categoryIconPath>
<userId>2</userId>
<categoryStatus>ON</categoryStatus>
<publicPrivate>Private</publicPrivate>
</Category>
......more categories
<Categorys>
Run Code Online (Sandbox Code Playgroud)
在这里,我希望将<categoryName>价值观纳入我的NSMutableArray categList.
我使用的代码是:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary*)attributeDict {
if ( [elementName isEqualToString:@"categList"]) {
// addresses is an NSMutableArray instance variable
if (!categList)
categList = [[NSMutableArray alloc] init];
return;
}
NSLog(@"StartedElement %@", elementName);
element = [NSMutableString string];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(element == nil)
element = [[NSMutableString alloc] init];
[element appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
NSLog(@"Found an element named: %@ with a value of: %@", elementName, element);
if ([elementName isEqualToString:@"categoryName"]) {
NSLog(@"\n\n\nfound::::::::::::::: %@", element);
category = element;
NSLog(@"category:::: %@", category);
}
[categList addObject:element];
NSLog(@"categList*************** %@", categList);
}
Run Code Online (Sandbox Code Playgroud)
但我没有得到我的类别名称NSMutableArray categList:
我的代码有什么问题?:(
谢谢
嘿你可以使用XMLReader,但它会返回NSDictionary.
以下代码演示XMLReader to get NSDictionary.
导入XMLReader您的文件.
#import "XMLReader.h"
Run Code Online (Sandbox Code Playgroud)
然后使用以下方法获取NSDictionary.在哪里你必须通过你XML的NSString
.
NSDictionary* xmlDict = [XMLReader dictionaryForXMLString:theXML error:&er];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11177 次 |
| 最近记录: |