xml解析iphone问题
我正在使用 NSXMLParser 但是我不太明白如何正确显示 xml 中的项目。例如,我在 xml 解析器中有这个:
- (void) parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"level4"]) {
//add a book object to the array at the index determined by bookCount
self.bookCount++;
[books addObject:[[book alloc]init]];
//[books insertObject:[[book alloc]init] atIndex:self.bookCount];
}
if ([elementName isEqualToString:@"module"]) {
isFirstName = YES;
}
if ([elementName isEqualToString:@"moduleTitle"]) {
isLastName = YES;
}
if ([elementName isEqualToString:@"semester"]) {
isTitle = YES;
}
if ([elementName isEqualToString:@"assessmentName"]) {
isGenre = YES;
}
}
这是我的 xml
<myCourse>
<courseName>BEng Mobile and Web Computing</courseName>
<courseStructure>
<level4>
<module>
<moduleCode>ECSC401</moduleCode>
<moduleTitle>Programming Methodology</moduleTitle>
<credits>15</credits>
<semester>1</semester>
<assessmentDetails>
<assessment>
<assessmentName>Test1</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>30</assessmentWeighting>
<assessmentDueDate/>
</assessment>
<assessment>
<assessmentName>Coursework</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>40</assessmentWeighting>
<assessmentDueDate/>
</assessment>
<assessment>
<assessmentName>Test2</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>30</assessmentWeighting>
<assessmentDueDate/>
</assessment>
</assessmentDetails>
</module>
</level4>
</courseStructure>
</myCourse>
(它使用相同的格式继续级别 1、级别 2、级别 3)。我只想显示“level4”层次结构下的所有模块 - 我该怎么做?/我做错了什么?这些项目显示的只是不正确的..
I'm using the NSXMLParser however i dont quite understand how to display items from a xml correctly. E.g. i have this in the xml parser:
- (void) parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"level4"]) {
//add a book object to the array at the index determined by bookCount
self.bookCount++;
[books addObject:[[book alloc]init]];
//[books insertObject:[[book alloc]init] atIndex:self.bookCount];
}
if ([elementName isEqualToString:@"module"]) {
isFirstName = YES;
}
if ([elementName isEqualToString:@"moduleTitle"]) {
isLastName = YES;
}
if ([elementName isEqualToString:@"semester"]) {
isTitle = YES;
}
if ([elementName isEqualToString:@"assessmentName"]) {
isGenre = YES;
}
}
This is my xml
<myCourse>
<courseName>BEng Mobile and Web Computing</courseName>
<courseStructure>
<level4>
<module>
<moduleCode>ECSC401</moduleCode>
<moduleTitle>Programming Methodology</moduleTitle>
<credits>15</credits>
<semester>1</semester>
<assessmentDetails>
<assessment>
<assessmentName>Test1</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>30</assessmentWeighting>
<assessmentDueDate/>
</assessment>
<assessment>
<assessmentName>Coursework</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>40</assessmentWeighting>
<assessmentDueDate/>
</assessment>
<assessment>
<assessmentName>Test2</assessmentName>
<assessmentType>Coursework</assessmentType>
<assessmentWeighting>30</assessmentWeighting>
<assessmentDueDate/>
</assessment>
</assessmentDetails>
</module>
</level4>
</courseStructure>
</myCourse>
(it continues level 1,level2,level3 using the same format). I simply want to display all the modules under the 'level4' hierarchy - how can i do this?/what am i doing wrong? the items are displaying just not the right ones ..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您必须创建一个 NSMutableDictionary。当您在 didStartElement 方法中获取标签时,启动此字典。并设置一个标志。
在 Found 字符方法中检查该标志,如果是则存储该 tagValues 并在 didEndElement 中存储该字典中的所有值。
您可以使用它找到所有级别的值。
希望这会对您有所帮助。
First of all you have to create one NSMutableDictionary. When you get the tag in didStartElement Method initiate this Dictionary.And also set one flag.
And in Found character method check that flag, if It is then store that tagValues and in didEndElement store all the values in this Dictionary when you find .
You can find all levels values using this.
Hope this will help you.
使用两种方法:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI QualifiedName:(NSString *)qName attribute:(NSDictionary *)attributeDict
和
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI QualifiedName:(NSString *)qName
用于解析。
Use both methods:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
and
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
For parsing.