如何通过xml解析器解析此xml文件中的属性 [英] how to parse the attributes like in this xml file through xml parser
问题描述
XML文件代码:
< Books>
< Book id =1isbn =123456>
< name>让我们C< / name>
< authors>
< author> Yashwant Kanetkar< / author>
< author> ABC< / author>
< / authors>
< price> 250.00< / price>
< / Book>
< Book id =2isbn =345678>
< name>使用C ++进行编程< / name>
< authors>
< author> Balaguruswamy< / author>
< author> XYZ< / author>
< / authors>
< price> 400.00< / price>
< / Book>
< Book id =3isbn =789012>
< name>专业的iPhone和iPad应用程序开发< / name>
< authors>
< author> Gene Backlin< / author>
< author> QWE< / author>
< / authors>
< price> 550.00< / price>
< / Book>
< Book id =4isbn =901234>
< name>开始iOS 4应用程序开发< / name>
< authors>
< author> Wei-Meng Lee< / author>
< author> IOP< / author>
< / authors>
< price> 500.00< / price>
< / Book>
< / Books>
任何人都可以帮我这个吗?
<我正在使用这段代码:
- (void)parser:(NSXMLParser *)解析器didStartElement:(NSString *) elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@Books])
{
appDelegate。 books = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:@Book])
{
aBook = [[book alloc] init];
aBook.bookID = [[attributeDict objectForKey:@id] integerValue];
aBook.isbn = [[attributeDict objectForKey:@isbn] integerValue];
NSLog(@阅读isbn价值:%@,aBook.isbn);
NSLog(@读取id值:%i,aBook.bookID);
}
NSLog(@处理元素:%@,elementName);
}
首先取一个bool变量。然后编写以下代码。也许它会帮助你我几天前完成了相关的工作。
第一个.h文件
@interface EventsViewController:UIViewController< NSXMLParserDelegate>
{
NSMutableArray * _idArray;
NSMutableArray * _isbnArray;
BOOL isBook;
}
然后在.m文件中你应该写这个。
//在ViewDidLoad中
- (void)viewDidLoad
{
[super viewDidLoad];
_idArray = [[NSMutableArray alloc] init];
_isbnArray = [[NSMutableArray alloc] init];
NSString * xmlString = [[NSBundle mainBundle] pathForResource:@today_in_history_shortofType:@xml];
NSURL * fileURL = [NSURL fileURLWithPath:xmlString];
NSXMLParser * parser = [[NSXMLParser alloc] initWithContentsOfURL:fileURL];
[parser setDelegate:self];
[解析器解析];
}
//现在使用xml解析器委托方法
pragma mark - NSXMLParser委托方法
- (void)解析器:(NSXMLParser *)解析器didStartElement: (NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@Books]){
isBook = YES;
}
else if([elementName isEqualToString:@Book]&& isBook){
NSString * idString = [attributeDict objectForKey:@id];
NSString * isbnString = [attributeDict objectForKey:@isbn];
[_idArray addObject:idString];
[_isbnArray addObject:isbnString];
NSLog(@Book id是:%@和Book isbn是:%@,idString,isbnString);
}
}
- (void)解析器:(NSXMLParser *)解析器foundCharacters:(NSString *)string {
}
- (void)解析器:(NSXMLParser *)解析器didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@Books ]){
isBook = NO;
NSLog(@Id数组计数:%d,[_ idArray计数]);
}
}
注意:我已经检查过,它正在工作,你可以在你的代码中使用它,如果它仍然不起作用,那么你的项目中会出现其他错误,你可以通过制作单独的项目来验证它。
XML FILE CODE:
<Books>
<Book id="1" isbn="123456">
<name>Let Us C</name>
<authors>
<author>Yashwant Kanetkar</author>
<author>ABC</author>
</authors>
<price>250.00</price>
</Book>
<Book id="2" isbn="345678">
<name>Programing With C++</name>
<authors>
<author>Balaguruswamy</author>
<author>XYZ</author>
</authors>
<price>400.00</price>
</Book>
<Book id="3" isbn="789012">
<name>Professional iPhone and iPad Application Development</name>
<authors>
<author>Gene Backlin</author>
<author>QWE</author>
</authors>
<price>550.00</price>
</Book>
<Book id="4" isbn="901234">
<name>Beginning iOS 4 Application Development</name>
<authors>
<author>Wei-Meng Lee</author>
<author>IOP</author>
</authors>
<price>500.00</price>
</Book>
</Books>
Can anyone pls help me with this?
I'm using this code:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"Books"])
{
appDelegate.books = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:@"Book"])
{
aBook = [[Book alloc] init];
aBook.bookID = [[attributeDict objectForKey:@"id"] integerValue];
aBook.isbn = [[attributeDict objectForKey:@"isbn"] integerValue];
NSLog(@"Reading isbn value : %@", aBook.isbn);
NSLog(@"Reading id value :%i", aBook.bookID);
}
NSLog(@"Processing Element: %@", elementName);
}
First take a bool variable. and then write the following code. perhaps it will help you I have done the related work , a few days ago.
First .h file
@interface EventsViewController : UIViewController <NSXMLParserDelegate>
{
NSMutableArray *_idArray;
NSMutableArray *_isbnArray;
BOOL isBook;
}
Then in .m file you should write this.
//In ViewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
_idArray = [[NSMutableArray alloc] init];
_isbnArray = [[NSMutableArray alloc] init];
NSString *xmlString = [[NSBundle mainBundle] pathForResource:@"today_in_history_short" ofType:@"xml"];
NSURL * fileURL = [NSURL fileURLWithPath:xmlString];
NSXMLParser * parser = [[NSXMLParser alloc] initWithContentsOfURL:fileURL];
[parser setDelegate:self];
[parser parse];
}
// Now In xml Parser Delegate Methods
pragma mark - NSXMLParser Delegate Method
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:@"Books"]) {
isBook = YES;
}
else if ([elementName isEqualToString:@"Book"] && isBook){
NSString *idString = [attributeDict objectForKey:@"id"];
NSString *isbnString = [attributeDict objectForKey:@"isbn"];
[_idArray addObject:idString];
[_isbnArray addObject:isbnString];
NSLog(@"Book id is: %@ and Book isbn is: %@",idString,isbnString);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"Books"]) {
isBook=NO;
NSLog(@"Id Array count is :%d",[_idArray count]);
}
}
Note: I have check it , It's working , you can use it in you code , if still it's won't working then there will be something else wrong in your project, you can verify it by making separate project.
这篇关于如何通过xml解析器解析此xml文件中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!