NsXmlParser无法正常工作

时间:2011-02-07 11:53:30

标签: objective-c xcode nsxmlparser

嗨我有一个非常奇怪的问题我有xml文件whit 21个元素,我想将它们添加到可变数组。解析器11次启动元素,11次foundCharacters但只有10次结束元素,它表现得像它结束xml文件。我的程序不会像它应该运行一样下降和运行。但有时它会使它应该如此,所以它读取所有21个元素。 为什么呢?

我的代码的一部分:

 /*this function start work whith xml file*/

- (void)parseXMLFile:(NSData *)adatok {
    if (adatok != nil) {
        BOOL success;
        if (addressParser) 
            [addressParser release];
        addressParser = [[NSXMLParser alloc] initWithData:adatok];
        [addressParser setDelegate:self];
        [addressParser setShouldResolveExternalEntities:YES];
        success = [addressParser parse]; 

    }
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
    NSLog(elementName);
    if ([elementName isEqualToString:@"turnover"]) {
        objektObrat =[[Obrat1 alloc] init]; 
        NSString *sth = [attributeDict objectForKey:@"transid"];
        objektObrat.uID = sth;
    }
    if ([elementName isEqualToString:@"amount"]) {
        osuma=YES;
    }
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
//filling the object which will be saved in database    
    NSLog(string);
    if (osuma) {
        osuma=NO;
        objektObrat.suma = [[NSString alloc] initWithString:string];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    NSLog(elementName);
    if ([elementName isEqualToString:@"turnover"]) {
        turnOver = NO;
        oid = NO;

        if (objektObrat != Nil){
            [obratDatabaseArray addObject:objektObrat];
            [objektObrat release];
        }
        //add to array
        [array addObject:vypisObratString];
    }

    if ([elementName isEqualToString:@"amount"]) {
        osuma=NO;
    }
}

我希望它有所帮助,至少有一个关键,我应该寻找问题 感谢

XML

<!ELEMENT turnovers (turnover*)>
<!ELEMENT turnover (amount, maturity-date, trans-date, counter-prefix, counter-account, counter-bank, counter-name, constant-symb, variable-symb, spec-symb, description, note, type, storno, rate, balance, currency, statement, message1, message2)> - detail obratu
<!ATTLIST turnover transid CDATA #REQUIRED>
<!ELEMENT amount (#PCDATA)> - suma
<!ELEMENT maturity-date (#PCDATA)> - dátum operácie
<!ELEMENT trans-date (#PCDATA)> - dátum transakcie
<!ELEMENT counter-prefix (#PCDATA)> - predčíslo protiúčtu
<!ELEMENT counter-account (#PCDATA)> - číslo protiúčtu
<!ELEMENT counter-bank (#PCDATA)> - kód banky
<!ELEMENT counter-name (#PCDATA)> - názov protiúčtu
<!ELEMENT constant-symb (#PCDATA)> - konštantný symbol
<!ELEMENT variable-symbol (#PCDATA)> - variabilný symbol
<!ELEMENT spec-symb (#PCDATA)> - špecifický symbol
<!ELEMENT constant-symb (#PCDATA)> - konštantný symbol
<!ELEMENT note (#PCDATA)> - poznamka
<!ELEMENT type (#PCDATA)> - typ obratu pozri číselník typy obratov v prílohe č. 1).
<!ELEMENT storno (#PCDATA)> - či je storno
<!ELEMENT rate (#PCDATA)> - kurz
<!ELEMENT balance (#PCDATA)> - zostatok
<!ELEMENT currency (#PCDATA)> - mena
<!ELEMENT statement (#PCDATA)> - číslo výpisu
<!ELEMENT message1 (#PCDATA)> - message 1
<!ELEMENT message2 (#PCDATA> - message 2

1 个答案:

答案 0 :(得分:0)

好吧,它可能是CDATA。如果你有一个有CDATA的标签,那么CData将通过函数

进行解析
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock

检查一下。