IOS study note 27-use gdataxml to parse XML documents

Source: Internet
Author: User

There are many ways to parse XML documents on the iOS platform. There are built-in parsing methods in the SDK, but in most cases, they tend to use third-party libraries, the reason is higher resolution efficiency, more convenient to use, on the iOS platform analysis of the advantages and disadvantages of various parsing XML library, you can see this article: http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project

Here we will mainly introduce gdataxml, an open-source library provided by Google for XML parsing on the iOS platform. You can go to idea (the gdataxml folder I created here ), note that you should select to copy the file to the project instead of just referencing it,



Configure the project, click the project root directory, and then click target on the left to go to build phases, and then click the third link binary with libraries, click the plus sign to search for libxml2 and add the Library to the project,



Next, go to build settings, search for head search path in the search box, double-click and click + to add/usr/include/libxml2,

Search for other linker flags in the search box, and add-lxml2 in the same way,

At this point, the work of adding and configuring is completed (it is a little troublesome), and then we will see how to use it:

First, create an XML file in the project as the object to be parsed. The new method is to create an empty file in the project, name it users. XML, and add the content:

<?xml version="1.0" encoding="utf-8"?><Users>    <User id="001"]]>        <name>Ryan</name>        <age>24</age>    </User>    <User id="002"]]>        <name>Tang</name>        <age>23</age>    </User></Users>

Next we can start parsing. Introduce the header file in the file to be parsed: # import "gdataxmlnode. H"

I am a new empty project, so I use it directly in appdelegate. m. The Code is as follows:

-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {self. window = [[[uiappswalloc] initwithframe: [[uiscreen mainscreen] bounds] autorelease]; // override point for customization after application launch. self. window. backgroundcolor = [uicolorwhitecolor]; [self. windowmakekeyandvisible]; // obtain the XML file nsstring * filepath = [[nsbundle mainbundle] pathforresource: @ "users" oftype: @ "XML"] in the project directory; nsdata * xmldata = [[nsdata alloc] initwithcontentsoffile: filepath]; // use the nsdata object to initialize gdataxmldocument * Doc = [[gdataxmldocument alloc] initwithdata: xmldata options: 0 error: Nil] // obtain the root node (users) gdataxmlelement * rootelement = [Doc rootelement]; // obtain the node (User) nsarray * Users = [rootelement elementsforname: @ "user"]; for (gdataxmlelement * user in users) {// nsstring * userid = [[user attributeforname: @ "ID"] stringvalue]; nslog (@ "User ID is: % @", userid); // obtain the value of the Name node gdataxmlelement * nameelement = [[user elementsforname: @ "name"] objectatindex: 0]; nsstring * name = [nameelement stringvalue]; nslog (@ "user name is: % @", name ); // obtain the value of the age node gdataxmlelement * ageelement = [[user elementsforname: @ "Age"] objectatindex: 0]; nsstring * age = [ageelement stringvalue]; nslog (@ "user age is: % @", age); nslog (@ "-----------------");} returnyes ;}

The output result of compilation and execution on the console is as follows:


If you are interested in Android and iOS, you can join our QQ discussion group. Here, we will only discuss the following:

IOS group: 220223507

Android group: 282552849


Welcome to my Sina Weibo chat: @ Tang Ren _ Ryan

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.