その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 私は結果を得ました余分なデータ。