我有和没有根元素的XML文件。我尝试过XMLParser.h - XMLParser.m文件和XMLReader.h - XMLReader.m。但我无法阅读我的所有元素。它只读了第一个元素。能帮我解决这个问题吗?
我的XML文件就像
<Firstname>First</Firstname>
<Lastname>Last</Lastname>
<Contactnumber>123456</Contactnumber>
<Address>Address of the person</Address>
我也尝试了NSXMLParser,但所有人都读了我的第一个元素。那只是
<Firstname>First<Firstname>
我该如何解决这个问题?我怎样才能阅读所有元素?
以下是用于XML阅读的方法
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
NSLog(@"%@ %@ %@ %@ %@" ,elementName,namespaceURI,qualifiedName,currentElementValue,attributeDict);
//NSLog(@"Processing Element: %@", elementName);
}
- (void)parser:(NSXMLParser *)parser foundAttributeDeclarationWithName:(NSString *)attributeName forElement:(NSString *)elementName type:(NSString *)type defaultValue:(NSString *)defaultValue
{
NSLog(@"element name -- %@ attribute -%@",elementName, attributeName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(@"Processing Value: %@", currentElementValue);
}
- (void)parserDidStartDocument:(NSXMLParser *)parser{
}
- (void)parserDidEndDocument:(NSXMLParser *)parser{
}
// sent when the parser has completed parsing. If this is encountered, the parse was successful.
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"affiliate_name"]) {
NSLog(@"Done --- ");
}
NSLog(@"%@ %@ %@ %@",elementName,namespaceURI,qName,currentElementValue);
defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:currentElementValue forKey:elementName];
[[NSUserDefaults standardUserDefaults] synchronize];
}
请帮我解决这个问题。提前致谢。