SlideShare ist ein Scribd-Unternehmen logo
1 von 19
iOS XML Parser
 Evan Johnson - Boniface Designs
Objective


Create and iPhone application that downloads
and parses XML data
Steps


Create our XML Data

Create a View-based Application
XML File
XML: Extensible Markup Language, a metalanguage that
allows users to define their own customized markup
languages.



  My XML file will contain the following:
    Root Element: <element>
    Child Element: <elements>
   Attribute: myData
XML File
 <?xml version=”1.0”?>
 <elements>
 <element myData=”XML Parsed Successfully” />
 </elements>


I am going to save this file as “xmltest.xml” and upload it to my server. If
you want, you can skip creating an XML file and use mine. It is located at:

http://www.bonifacedesigns.com/tuts/xmltest.xml
iOS Methods

NSURLConnection
The NSURLConnection will allow us to download the XML file and
communicate with the server.


NSXML Parser
The NSXMLParser methods will help us access the data stored in the
XML file.


      Delegate: NSXMLParserDelegate
iOS Properties

NSMutableData
We will create an NSMutableData object to store the XML data that we
download


UILabel
I will present the downloaded data in a label on the applications screen


UIButton
A UIButton tap action will start the connection to download the XML
data
Steps
Now that we have created our app, we need
to make a connection to load our XML data.
We will do that with the following:

  NSURLRequest
  I will store the following connection data in a NSURLRequest object:
  URL Path to XML file, cache policy, and the timeout interval.


  NSURLConnection
  After creating the NSURLRequest object, I will initiate my connection
Make the request
 // Request data from URL
 NSURLRequest *theRequest = [NSURLRequest requestWithURL:
 [NSURL URLWithString:[NSString stringWithFormat:@"http://
 www.bonifacedesigns.com/tuts/xmltest.xml"]]
 
 
   
 
 
 
 
 
 
 
 
 
 cachePolicy:NSURLRequestUseProtocolCachePolicy
 
 
   
 
 
 
 
 
 
 
 
 timeoutInterval:60.0];




Above is the NSURLRequest. Notice that the request utilizes a URL from a
string. The URL is where the XML file I created earlier is hosted. I am using
default settings for the cache policy and time out interval.
Start the connection
    // Start loading data
    NSURLConnection *theConnection = [[NSURLConnection alloc]
    initWithRequest:theRequest delegate:self];
    if (theConnection) {
         // Create the NSMutableData to hold data
    
 receivedData = [[NSMutableData data] retain];
    } else {
    
 // Inform the user the connection failed.
    }


Now I need to allocate the NSURLConnection and check if it was created. If so,
I will create my NSMutableData object to hold any data that I receive. If not,
you can call upon another method and/or inform the user.

I am going to place this code and the NSURLRequest code in a method that
will be called when my UIButton is tapped.
Connection Response
     Received
 - (void)connection:(NSURLConnection *)connection
 didReceiveResponse:(NSURLResponse *)response



This response is usually received before any data has been loaded. I am
going to reset my NSMutableData object encase the server reset in the
middle of of my connection.
Connection Received
       Data
 - (void)connection:(NSURLConnection *)connection
 didReceiveData:(NSData *)data



Our XML data might be loaded incrementally. This response is called
whenever data is loaded. I need to append this data to my NSMutableData
object.
Connection Failed!
        (uh-oh)
 - (void)connection:(NSURLConnection *)connection
 didFailWithError:(NSError *)error



When a connection fails, this method will be called. We need to release our
connection, release our NSMutableData object, and possibly inform the
user.
Connection Finished

 - (void)connectionDidFinishLoading:(NSURLConnection
 *)connection



If everything works properly and our data is successfully loaded, this
method will be called. We need to initiate our parser (which I will do in a
separate method), and release our connection.
Steps


Create our data parser

Parse our data

Handle the data
Initiate our parser

 NSXMLParser *dataParser = [[NSXMLParser alloc]
 initWithData:receivedData];



In a method I will create (called startParsingData:), I will initiate the XML
parser with the method above. I will also assign a delegate to the parser
(self), tell the parser to parse my data, release the parser, and release the
NSMutableData object containing my data.
Parse Our Data
 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString
 *)elementName namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName attributes:(NSDictionary
 *)attributeDict



The above method will be called once our data is parsed. The method
gives us access to a number of variables, but we are mainly interested in
the elementName and attributeDict. The elementName is where we can
access the XML root and/or child element names. The attributeDict is a
NSDictionary that will store all of the attributes for our elements.
Parser Failed

 - (void)parser:(NSXMLParser *)parser parseErrorOccurred:
 (NSError *)parseError




The above method will be called if the parser has any errors. If the parser
fails, you can then notify the user and/or call upon another method. We
will just be notifying the user with the UILabel we created earlier.
Finally, connect
      everything up

 Open up our xib file and add a UIButton and
UILabel. Connect these objects to their respective
                   IBOutlets.

Weitere ähnliche Inhalte

Andere mochten auch

COGITO Classic Tutorial (Android)
COGITO Classic Tutorial (Android)COGITO Classic Tutorial (Android)
COGITO Classic Tutorial (Android)Cookoo Tips
 
iOS Development - A Beginner Guide
iOS Development - A Beginner GuideiOS Development - A Beginner Guide
iOS Development - A Beginner GuideAndri Yadi
 
SAP CRM Technical Course Content
SAP CRM Technical Course ContentSAP CRM Technical Course Content
SAP CRM Technical Course ContentChoodamani Infotech
 
Introduction to iOS Development
Introduction to iOS DevelopmentIntroduction to iOS Development
Introduction to iOS DevelopmentAsim Rais Siddiqui
 
JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The BasicsJeff Fox
 

Andere mochten auch (8)

COGITO Classic Tutorial (Android)
COGITO Classic Tutorial (Android)COGITO Classic Tutorial (Android)
COGITO Classic Tutorial (Android)
 
iOS Development - A Beginner Guide
iOS Development - A Beginner GuideiOS Development - A Beginner Guide
iOS Development - A Beginner Guide
 
SAP CRM Technical Course Content
SAP CRM Technical Course ContentSAP CRM Technical Course Content
SAP CRM Technical Course Content
 
Introduction to iOS Development
Introduction to iOS DevelopmentIntroduction to iOS Development
Introduction to iOS Development
 
Json
JsonJson
Json
 
Apple iOS Report
Apple iOS ReportApple iOS Report
Apple iOS Report
 
JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The Basics
 
Apple iOS
Apple iOSApple iOS
Apple iOS
 

Kürzlich hochgeladen

Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 

Kürzlich hochgeladen (20)

Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 

iOS XML Parsing Tutorial

  • 1. iOS XML Parser Evan Johnson - Boniface Designs
  • 2. Objective Create and iPhone application that downloads and parses XML data
  • 3. Steps Create our XML Data Create a View-based Application
  • 4. XML File XML: Extensible Markup Language, a metalanguage that allows users to define their own customized markup languages. My XML file will contain the following: Root Element: <element> Child Element: <elements> Attribute: myData
  • 5. XML File <?xml version=”1.0”?> <elements> <element myData=”XML Parsed Successfully” /> </elements> I am going to save this file as “xmltest.xml” and upload it to my server. If you want, you can skip creating an XML file and use mine. It is located at: http://www.bonifacedesigns.com/tuts/xmltest.xml
  • 6. iOS Methods NSURLConnection The NSURLConnection will allow us to download the XML file and communicate with the server. NSXML Parser The NSXMLParser methods will help us access the data stored in the XML file. Delegate: NSXMLParserDelegate
  • 7. iOS Properties NSMutableData We will create an NSMutableData object to store the XML data that we download UILabel I will present the downloaded data in a label on the applications screen UIButton A UIButton tap action will start the connection to download the XML data
  • 8. Steps Now that we have created our app, we need to make a connection to load our XML data. We will do that with the following: NSURLRequest I will store the following connection data in a NSURLRequest object: URL Path to XML file, cache policy, and the timeout interval. NSURLConnection After creating the NSURLRequest object, I will initiate my connection
  • 9. Make the request // Request data from URL NSURLRequest *theRequest = [NSURLRequest requestWithURL: [NSURL URLWithString:[NSString stringWithFormat:@"http:// www.bonifacedesigns.com/tuts/xmltest.xml"]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; Above is the NSURLRequest. Notice that the request utilizes a URL from a string. The URL is where the XML file I created earlier is hosted. I am using default settings for the cache policy and time out interval.
  • 10. Start the connection // Start loading data NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if (theConnection) { // Create the NSMutableData to hold data receivedData = [[NSMutableData data] retain]; } else { // Inform the user the connection failed. } Now I need to allocate the NSURLConnection and check if it was created. If so, I will create my NSMutableData object to hold any data that I receive. If not, you can call upon another method and/or inform the user. I am going to place this code and the NSURLRequest code in a method that will be called when my UIButton is tapped.
  • 11. Connection Response Received - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response This response is usually received before any data has been loaded. I am going to reset my NSMutableData object encase the server reset in the middle of of my connection.
  • 12. Connection Received Data - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data Our XML data might be loaded incrementally. This response is called whenever data is loaded. I need to append this data to my NSMutableData object.
  • 13. Connection Failed! (uh-oh) - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error When a connection fails, this method will be called. We need to release our connection, release our NSMutableData object, and possibly inform the user.
  • 14. Connection Finished - (void)connectionDidFinishLoading:(NSURLConnection *)connection If everything works properly and our data is successfully loaded, this method will be called. We need to initiate our parser (which I will do in a separate method), and release our connection.
  • 15. Steps Create our data parser Parse our data Handle the data
  • 16. Initiate our parser NSXMLParser *dataParser = [[NSXMLParser alloc] initWithData:receivedData]; In a method I will create (called startParsingData:), I will initiate the XML parser with the method above. I will also assign a delegate to the parser (self), tell the parser to parse my data, release the parser, and release the NSMutableData object containing my data.
  • 17. Parse Our Data - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict The above method will be called once our data is parsed. The method gives us access to a number of variables, but we are mainly interested in the elementName and attributeDict. The elementName is where we can access the XML root and/or child element names. The attributeDict is a NSDictionary that will store all of the attributes for our elements.
  • 18. Parser Failed - (void)parser:(NSXMLParser *)parser parseErrorOccurred: (NSError *)parseError The above method will be called if the parser has any errors. If the parser fails, you can then notify the user and/or call upon another method. We will just be notifying the user with the UILabel we created earlier.
  • 19. Finally, connect everything up Open up our xib file and add a UIButton and UILabel. Connect these objects to their respective IBOutlets.