0

そのxmlファイルには4つのカテゴリタグとその他のタグが含まれています.xmlファイルからカテゴリタグのデータを取得したい.カテゴリデータを取得するときは、毎回カテゴリ値の配列を出力します。正しく表示されます。しかし、解析が完了すると、最後のカテゴリ名に別のタグ データが追加されます。解析コードは次のようになります。

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if(self.presentvalue)
{
    [presentvalue appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{

if(![elementName compare:@"category"])
{
    [categories addObject:presentvalue];
    NSLog(@"%@",presentvalue);
    NSLog(@"Number of elements %@",categories);
}

}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"category"])
{
    presentvalue=[NSMutableString string];  
}
}

そして、私のxmlファイルの内容は次のようになります

    <lesson>
   <categories>
   <category id="1">1</category>
   <category id="2">2</category>
   <category id="3">3</category>
   </categories>
   <references>
    <reference id="1" type="youtube" categoryid="2" name="Boyles law">
    <url><![CDATA[http://www.youtube.com/watch?v=J_I8Y-i4Axc]]>
     </url>
    </reference>
    </references>
    </lesson>

この xml から、1,2,3 のような配列値を取得しました http://www.youtube.com/watch?v=J_I8Y-i4Axc.Like this 私は結果を得ました余分なデータ。

4

3 に答える 3

0
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"category"]) {
    NSLog(@"Node is found correctly");
    if(presentvalue == nil)
        presentvalue = [NSMutableString string];
    else 
        [presentvalue setString:@""];
}
else {
    presentvalue = nil;
}

}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{

[presentvalue appendString:string];
if (presentvalue) 
{
   [self.tempArray addObject:presentvalue]; // add this line. create a tempArray containing all the strings.
    presentvalue = nil;
}
}

これはあなたの解析に役立つはずです。それが役に立てば幸い。幸せなコーディング:)

于 2012-05-10T07:25:14.597 に答える
0

xml 解析コードにチェックを入れます。開始/終了要素名がカテゴリの場合。オブジェクトを配列に追加します。それ以外は何もしません。ただのパスアウト

于 2012-05-10T07:15:41.417 に答える
0

この線は奇妙に見えると思います

if(![elementName compare:@"category"])

こんなはずじゃないの?(それなし !)

if([elementName compare:@"category"])

編集済み

これを didEndElement に置き換えてみてください

if([elementName isEqualToString:@"category"])
{
    [categories addObject:presentvalue];
    presentValue = [NSMutableString string];
    NSLog(@"%@",presentvalue);
    NSLog(@"Number of elements %@",categories);
}
于 2012-05-10T07:41:53.010 に答える