xml解析iphone问题

发布于 2024-11-09 05:19:14 字数 2753 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

做个少女永远怀春 2024-11-16 05:19:14

首先,您必须创建一个 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.

荒人说梦 2024-11-16 05:19:14

使用两种方法:

- (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文