How To load XML file into iPhone project

2019-08-01 02:41发布

问题:

I have an XML documment that I want to load for the iPhone, do I need to convert it to a plist first ? if so how ?

The xml document has the following code ( for 1 chapter)

<toolTipsBook>
−  <chapter index="1" name="Chapter Name">
<line index="1" text="line text here"/>
<line index="2" text=" line text here "/>
<line index="3" text=" line text here "/>
<line index="4" text=" line text here "/>
<line index="5" text=" line text here "/>
<line index="6" text=" line text here "/>
<line index="7" text=" line text here "/>
</chapter>

How can I tell xcode to display chapter 1 line 1 and then leave space under that for my comment ( a seperate xml document) for chapter l line 1 directly under it.

The idea is that I'll have this control for all the chapters in the data I'm loading.

If you have a little time I'd really appreciate it if you could give some sample could to please show what you mean.

Thanks guys,

回答1:

You can add the XML file into resources of your application.

You do that by dragging to resources directory in Xcode, and in popup select copy file to project.

When your application run you open the file by referring to it by it's name, read in the XML, parse it with NSXMLParser and extract required data. No need to convert to plist.

This assumes that the xml data is static and you don't indent to update and save it.



回答2:

Have you looked at NSXMLParser?

It's purpose is to parse the XML into a data structure.

Then you display that data structure using whatever user interfaces you feel are appropriate (such as a UITableView).