我从我的服务器返回了这个相当复杂的XML,我想帮助将它解析为一个我可以访问的对象供以后使用。
这就是xml的样子
<Eng>
<Result >
<Series >
<Link/>
<FMF/>
<AlsoLink/>
<Plugins/>
</FMF>
<Sheet >
<Spaces>
<Spacing >
<Names/>
</Spacing>
<Spacing >
<Names/>
</Spacing>
</Spaces>
</Sheet>
</Series>
</Result>
</Eng>
然后我使用NSXMLParser尝试将xml中每个元素的所有“ObjectForKey / s”解析为他们自己的dictionarys ..如果这是有道理的。
这就是我的代码目前的样子
#pragma mark - Parsing lifecycle
- (void)startTheParsingProcess:(NSData *)parserData
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:parserData]; //parserData passed to NSXMLParser delegate which starts the parsing process
[parser setDelegate:self];
[parser parse]; // starts the event-driven parsing operation.
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"Series"]){
parsedMutableDictionary = [[NSMutableDictionary alloc] initWithDictionary:attributeDict];
self.parsedDataArrayOfDictionaries = [NSMutableArray arrayWithCapacity:8];
if ([elementName isEqualToString:@"Link"]) {
}
if ([elementName isEqualToString:@"FMF"]) {
if ([elementName isEqualToString:@"AlsoLink"]) {
}
else if ([elementName isEqualToString:@"Plugins"]) {
}
}
else if ([elementName isEqualToString:@"Sheet"]) {
if ([elementName isEqualToString:@"Spaces"]) {
if ([elementName isEqualToString:@"Spacing"]) {
if ([elementName isEqualToString:@"Names"]) {
}
}
}
}
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@"Series"]){
[parsedDataArrayOfDictionaries addObject:parsedMutableDictionary];
if ([elementName isEqualToString:@"Link"]) {
}
if ([elementName isEqualToString:@"FMF"]) {
if ([elementName isEqualToString:@"AlsoLink"]) {
}
else if ([elementName isEqualToString:@"Plugins"]) {
}
}
else if ([elementName isEqualToString:@"Sheet"]) {
if ([elementName isEqualToString:@"Spaces"]) {
if ([elementName isEqualToString:@"Spacing"]) {
if ([elementName isEqualToString:@"Names"]) {
}
}
}
}
}
// parsedMutableDictionary = nil;
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
NSLog(@"Paser Error = %@", parseError);
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"A parsing failure occurred." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[errorAlert show];
}
//TODO: Look into how to use this method over multiple views. i.e.(other requests such as keycode, advanced )
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
// display values
NSArray *filteredArray = parsedDataArrayOfDictionaries;
NSLog(@"%@", filteredArray);
}
我遇到的问题是我不明白我应该在他们自己的对象中拥有什么...当其中一些值返回时我可能有多个Spacing元素而我只是不知道如何处理... < / p>
任何帮助或提示都会非常受欢迎,如果我没有指明一些东西只是问,因为我的大脑正在努力解决这个问题。
感谢。
答案 0 :(得分:2)
假设您的XML文件....
<Series element1="something" element2="something">
<Spaces>
<Spacing>
<Names>
Something
</Names>
</Spacing>
<Spacing>
<Names>Something</Names>
</Spacing>
</Spaces>
</Series>
要获得element1的值,你必须做.....
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([currentElement isEqualToString:@"Series"]) {
NSLog(@"%@",[attributeDict objectForKey:@"element1"]);
}
}
要获得间距的多个值,您必须执行....
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if([currentElement isEqualToString:@"name"]) {
[currentSpacingName appendString:string];
[currentSpacingName appendString:@"any character"]
}
}
之后将该值存储到此方法中带有欲望键的字典中......
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
这只是一个例子,我希望你现在可以解决你的问题。
答案 1 :(得分:0)
您正在使用NSXMLParser ....因此您将获得标记之间的字符串... <tag>strings</tag>
要获取多个Spacing元素,您可以将所有字符串附加到NSString对象中。