iPad XCode 中的 NSXMLParser

发布于 2024-11-08 20:06:57 字数 2313 浏览 2 评论 0原文

我正在尝试从 NSXMLParser 获取数据

,我有以下 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>

并且我创建了两个实体类:

Module.h

@interface Module : NSObject {

    NSString *moduleCode;
    NSString *moduleTitle;
    NSString *credits;
    NSString *semester;

}

AssessmentDetail.h

@interface AssessmentDetail : NSObject {

    NSString *assessmentName;
    NSString *assessmentType;
    NSString *assessmentWeighting;
    NSString *assessmentDueDate;

}

这就是我所拥有的一切,我希望能够解析XML 数据放入数组中以实现到 UITableView 中。

任何人都可以指导我或帮助我通过一种简单的方法来实际输出数据吗?

多谢。

I'm trying to get hold of data from a NSXMLParser

I have the following XML Sample Data:

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

And I have created the two Entity classes:

Module.h

@interface Module : NSObject {

    NSString *moduleCode;
    NSString *moduleTitle;
    NSString *credits;
    NSString *semester;

}

AssessmentDetail.h

@interface AssessmentDetail : NSObject {

    NSString *assessmentName;
    NSString *assessmentType;
    NSString *assessmentWeighting;
    NSString *assessmentDueDate;

}

That's all I have really, I would like to be able to parse the XML data into an array to be implemented into a UITableView.

Can anyone guide me or help me with a simple way to actually have data output?

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

沉默的熊 2024-11-15 20:06:58

你真的想使用 NSXMLParser 吗?如果没有,我建议使用TBXML。如果您了解 XML 文件的结构,那么使用起来非常容易。从本页开始获取 API:TBXML API

如果您熟悉 Objective -c 您将在半小时内解析此 XML。 ;-)

桑德罗·迈耶

Dou you really want to use NSXMLParser? If not, I suggest to use TBXML. It's really easy to use if you know the structure of your XML File. Start at this page to get the APIs: TBXML APIs

If you are familiar with Objective-c you'll get this XML parsed in half an hour. ;-)

Sandro Meier

半岛未凉 2024-11-15 20:06:58

为什么可以使用像 touchXml 这样的简单解析器?

您可以轻松解析并设置所有变量/数组以传递到表中。

Why you can use a simple parser like touchXml?

You can parse easily and set all of your variables / array to pass in your table.

溺深海 2024-11-15 20:06:58

非常易于使用的 NSXMLParser 是一个事件驱动的解析器,这意味着当解析器遍历 XML 数据时,它会为解析器找到的每个元素创建一个事件。这些事件作为消息发送到 NSXMLParser 的委托。该委托是您必须编写的符合 NSXMLParserDelegate 协议的类。

为了给出此 XML 数据的示例,解析器将在找到 后向委托发送一条 parser:didStartElement:namespaceURI:qualifiedName:attributes: 消息,然后找到 Test1 时发出 parser:foundCharacters: 消息,然后找到 时发出 parser:didEndElement:namespaceURI:qualifiedName: 消息。。您的委托对象应该维护状态信息。 didStartElement 消息应设置状态以了解下一个字符应保存在 AssessmentDetail 对象的 assessmentName ivar 中。

您的委托不需要实现 NSXMLParserDelegate 协议中的所有方法。只需使用这三种方法就可以摆脱困境。 didStartElement 元素需要了解大约十个元素,对应于您的两个数据对象及其 ivars,并适当地更改状态,以便您的 parser:foundCharacters: 方法将正确对象中的数据。

The very easy to use NSXMLParser is an event driven parser, meaning that as the parser goes through XML data it creates an event for each element the parser finds. These events are sent as messages to the delegate of the NSXMLParser. That delegate is a class you'll have to write that conforms to the NSXMLParserDelegate protocol.

To give an example for this XML data, the parser would send the delegate a parser:didStartElement:namespaceURI:qualifiedName:attributes: message upon finding <assessmentName>, then a parser:foundCharacters: message upon finding Test1, then a parser:didEndElement:namespaceURI:qualifiedName: upon finding </assessmentName>. Your delegate object should maintain state information. The didStartElement message should set the state to know that the next characters should be saved in a AssessmentDetail object's assessmentName ivar.

Your delegate need not implement all the methods in the NSXMLParserDelegate protocol. You could get away with just these three methods. The didStartElement element would need to know about ten elements, corresponding to your two data objects and their ivars, and change state appropriately, so that your parser:foundCharacters: method will put the data in the correct object.

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