XML Data Interchange Format in IOS

Source: Internet
Author: User
Tags xml parser

XML is a self-describing data interchange format, but the XML data Interchange format does not have the portability of JSON, but admittedly, XML is also widely used, such as the use of XML in ROS (an open-source robotic operating system) to describe the dependencies of packages, and of course, many more.

For XML document operations including "read" and "write", the process of reading into XML documents and parsing is called "parsing" XML documents, "parsing" XML documents in actual development accounted for a large proportion.

There are two modes of reading and writing XML documents that are currently popular: SAX and DOM. SAM is an event-driven parsing pattern. When parsing XML, the program reads the XML document from top to bottom, and if it encounters a start tag, an end tag, a property, and so on, it triggers the corresponding event, but one drawback of parsing an XML file is that it can only read an XML document and not write an XML document. But there are also advantages, that is, the resolution is fast, and iOS is recommended to use the SAX parsing mode.

The DOM pattern is to parse an XML document as a tree structure, providing the contents of the Get node, as well as related attributes, or adding, deleting, and modifying the contents of the node. XML parser after the XML file is loaded, the DOM treats the elements of the XML file as a tree-like node, which is read into memory at once. If the document is large, the parsing speed will be slow, which is unavoidable. But one of the DOM patterns is that SAX cannot be replaced by the DOM's ability to modify XML documents.

Nsxml

Nsxml is the default parsing framework of Apple's own, using SAX mode for parsing. It is a representation of the SAX parsing pattern. The core of the Nsxml framework is the Nsxmlparser and its delegated protocol nsxmlparserdelegate. The main parsing work is done in the implementation class of the delegate protocol, where many callback methods are defined, and when the SAX parser traverses the XML document from top to bottom, it encounters a start tag, an end tag, a document start, a document end, and a string that trigger the relevant method.

Common methods:

(1) parserdidstartdocument--triggers when the document starts

(2) Parser:didStartElement:namespaceURI:qualifiedName:attributes--starts triggering when a start tag is encountered, where the NamespaceURI part is the namespace, QualifiedName is a qualified name, attributes is a collection of properties of the dictionary type

(3) Parser:foundcharacters--triggered when a string is encountered

(4) Parser:didEndElement:namespaceURI:qualifiedName-triggers when an end tag is encountered

(5) Parserdidenddocument-triggered at the end of a document

The following implements a parsed Xmlparser class.

XMLParser.h

#import <Foundation/Foundation.h> @interface xmlparser:nsobject <nsxmlparserdelegate>//start parsing-(void) Start; @end
Xmlparser.m

-(void) start{    nsstring* path = [[NSBundle mainbundle] pathforresource:@ "Test" oftype:@ "xml"];     Resolves a document for a test.xml file        nsurl *url = [Nsurl Fileurlwithpath:path];    Start parsing xml    Nsxmlparser *parser = [[Nsxmlparser alloc] initwithcontentsofurl:url];    Parser.delegate = self;    [parser parse];    NSLog (@ "Parse complete ...");

If there are shortcomings also hope to point out!


XML Data Interchange Format in IOS

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.