Parsing XML code on iphone SDK

后端 未结 2 1047
陌清茗
陌清茗 2020-12-22 11:33

I have written some code, most of which has been taken from tutorials etc, but not sure it\'s 100%. The code works, but I think its a bit long winded, and has memory leaks.<

2条回答
  •  情话喂你
    2020-12-22 11:51

    1. Unfortunately the NSXMLParser that Apple provides requires at least 3 functions to work, as you have done. If you want to clean it up, think about using a third party script, such as TouchXML or KissXML.

    2. As for making this dynamic, you can put those numbers into an array and read them out, or change the way you're setting the data. Usually with different sections you use different arrays for the data. This takes your theRow variable out of the equation. Here is a tutorial that uses multiple arrays without a dictionary.

    3. @JeremyP has some good suggestions. If you're still struggling, do a Build and Analyze. This will show you "potentially leaked objects" so you can go through and make sure everything's released properly.


    There are 2 ways to go about parsing XML Trees-which is what you are doing.

    The first is using the NSXML parser you already have. First create a dictionary containing all of the higher level element names, i.e. section and item, then add two extra strings to your parser, currentNodeName and parentNodeName. As you iterate through, change the currentNodeContent to the elementName. If the elementName is in your dictionary, set parentNodeName to the elementName, otherwise leave it as is.

    Using TouchXML requires a bit less code. It works with xpaths, so you can specify /section/name/item/title in a path format. You will probably need to add another for loop that counts because you drill down another level.

提交回复
热议问题