English 中文(简体)
使用XML(电话/iOs)的字典
原标题:Using dictionaries with XML (iphone/iOs)

我想从Xml文档中整理数据。 迄今为止,这项工作仍在进行之中,但我没有找到从字典获取数据的正确途径。

我的格言如下:

    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    attributes:(NSDictionary *)attributeDict{ 
    currentElement = [elementName copy];
    if ([elementName isEqualToString:@"Placemark"]) {
        placemarkData = [[NSMutableDictionary alloc] init];
        currentTitle = [[NSMutableString alloc] init];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if ([currentElement isEqualToString:@"name"] || [currentElement isEqualToString:@"address"] || [currentElement isEqualToString:@"coordinates"])
        [currentTitle appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName{ 
    if ([elementName isEqualToString:@"Placemark"]) {
        [placemarkData setObject:currentTitle forKey:currentElement];
        [Placemarks addObject:[placemarkData copy]];
        NSLog(@"adding story: %@", currentElement);
    }

你可以看到。 数据储存在一个字典上,该字典存放在一阵列中。 只有问题才是字典的关键。 这对我来说是合乎逻辑的,但我看不出正确执行它的逻辑。 有些人看着吗?

页: 1

最佳回答

当你填写字典时,你就把物体放在钥匙上。 为了从dict子中获取物体,你需要钥匙。 我认为,这是你的问题。

为了把钥匙列入你的字典,你可以使用一个关键数字:

NSEnumerator *enumerator = [myDictionary keyEnumerator];
id key;

while ((key = [enumerator nextObject])) {

    /* code that uses the returned key */
    NSString *theElement = key;
    NSLog(@"Element: %@", theElement);
}

根据这一法典,你将列出所有关键因素。 为找到标的物,请使用<代码>[米Dictionary ObjectForKey:key];。 如果你知道你的XML中的内容,你就可以按照这一思路接触他们。

问题回答

我认为,你面临的问题是你所用的逻辑。

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    attributes:(NSDictionary *)attributeDict{ 
    currentElement = [elementName copy];
    if ([elementName isEqualToString:@"Placemark"]) {
        placemarkData = [[NSMutableDictionary alloc] init];
        currentTitle = [[NSMutableString alloc] init];
    }
}

方法,即分配“地方标志”,如果第一次碰到点码,但就下一个点而言,会遇到问题,则将处以罚款。

如果存在任何疑问或疑问,就会回头。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签