iOS parsing XML data

Source: Internet
Author: User

The class for parsing XML data in iOS is nsxmlparseR, which is detailed using the following methods:

    • Suppose you now have an XML binary data object (NSData) in Memory: data (which may be from a network or local file data), set the protocol for NSXMLParseR objects, with the following code:

  NSXMLParser *xmlparse = [[Nsxmlparser alloc] initwithdata:data];

  [Xmlparse setdelegate:self];

[Xmlparse parse];// parsing begins

    • after parsing the object settings, we need to implement the relevant proxy protocol: nsxmlparserdelegate. To implement the Proxy method in this protocol, there are several necessary methods:

parsing begins, looking for elements that need to be parsed

-(void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) QName attributes: (nsdictionary *) attributedict

{

TODO: Gets the value of the address element property of value

if ([@ "Address" isequaltostring:elementname])

{

Self.xmlelementname = elementname;

if (attributedict && [attributedict objectforkey:@ "value"])

{

Self.xmlparseresult = [nsmutablestring string];

[Self.xmlparseresult appendformat:@ "%@", [attributedict objectforkey:@ "value"];

}

}

}

-(void) Parser: (Nsxmlparser *) parser foundcharacters: (NSString *) string

{

if (!self.xmlelementname)

{

Return

}

}

parse an element end

-(void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) qName

{

if ([@ "Address" isequaltostring:elementname])

{

Self.xmlelementname = nil;

}

}

parse the completion of the document, where the results of the resolution can be processed

-(void) Parserdidenddocument: (Nsxmlparser *) parser

{

Todo:xml parsing complete, results stored in Self.xmlparseresult

if (!self.xmlparseresult | | [@ "" isEqualToString:self.xmlParseResult])

{

The analytical results can be further processed here

}

}

"!--endfragment-->

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.