iPad XCode 中的 NSXMLParser
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你真的想使用 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
为什么可以使用像 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.
非常易于使用的
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 theNSXMLParser
. That delegate is a class you'll have to write that conforms to theNSXMLParserDelegate
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 aparser:foundCharacters:
message upon findingTest1
, then aparser:didEndElement:namespaceURI:qualifiedName:
upon finding</assessmentName>
. Your delegate object should maintain state information. ThedidStartElement
message should set the state to know that the next characters should be saved in aAssessmentDetail
object'sassessmentName
ivar.Your delegate need not implement all the methods in the
NSXMLParserDelegate
protocol. You could get away with just these three methods. ThedidStartElement
element would need to know about ten elements, corresponding to your two data objects and their ivars, and change state appropriately, so that yourparser:foundCharacters:
method will put the data in the correct object.