Dom is a set of specifications specified by the list, the core is to process the data in a tree structure, the DOM parser reads the XML file and constructs an identical "tree" in memory, the tree nodes and XML tags correspond to manipulate the "tree" to work with the files in the XML. When the XML file is large, the established "tree" is also large, so it takes up a lot of memory.
The core of the SAX parser is the event handling mechanism. For example, when the parser discovers the start tag of a tag, the discovered data is encapsulated as a token start event, which is reported to the event handler, and the event handler re-call method (Startelement) processes the discovered data. Let's try sax and Dom parsing:
The XML format is as follows:
1, we use SAX parsing with the Apple Nsxmlparser, first initialize a parser, by setting up the agent to start parsing, the code is as follows:
Initialization path NSString * path=[[nsbundle Mainbundle] pathforresource:@ "Test.xml" oftype:nil]; NSData * Data=[nsdata Datawithcontentsoffile:path]; Initialize Parser Nsxmlparser * Parser=[[nsxmlparser alloc] initwithdata:data];//set agent parser.delegate=self;//start parsing [parser parse ];
2, through the proxy method to start parsing, because the SAX mode is event-driven, so parsing is in order to parse, if you want to get the label data we need to monitor the parsing of each step, so the system provides proxy method to us, the following is the common proxy method:
Start loading document-(void) Parserdidstartdocument: (Nsxmlparser *) parser{}//End loaded document-(void) Parserdidenddocument: (Nsxmlparser *) parser{}//Start parsing label-(void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (nsstring *) NamespaceURI QualifiedName: (NSString *) QName attributes: (nsdictionary *) attributedict{}//label parsing End-(void) parser: ( Nsxmlparser *) Parser didendelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: ( NSString *) qname{}//read text between tags-(void) Parser: (Nsxmlparser *) parser foundcharacters: (NSString *) string{content= string;}
3, XM has two methods of deposit, one is a property, and one is the text between the tags. If we want to get properties that need to be obtained in the proxy method,
If you are getting the text between tags, you need to use the proxy method
Summary: With a given proxy approach we can easily get the content of XML
1, we use the DOM parsing with the third-party framework Gdataxmlnode, the framework is a dynamic library. We need to introduce the following:
There is also a need to introduce a third-party support framework:
Dom parsing is a little simpler than sax, where the DOM first puts the entire file into memory. The contents of the elements are then retrieved in memory as a two-fork tree.
The main classes involved are:
Gdataxmldocument//Entire document
Gdataxmlelement//Node object
Gdataxmlnode//Attribute Node object
We can parse the data in the following ways
Initialize//Add file to memory Gdataxmldocument * Document=[[gdataxmldocument alloc] initwithdata:data options:0 Error:nil]; Gets the root element gdataxmlelement * root=document.rootelement;//gets all child nodes of that name-(Nsarray *) Elementsforname: (NSString *) name;//gets the property value by name-(gdataxmlnode*) Attributeforname: (NSString *) name;
So that we can easily parse and get the value of the XML,
Dom and Sax parsing methods for XML in IOS development