iOS Learning (OC language) Knowledge Point finishing
I. Introduction of XML data format
1) Concept: XML is a markup language for Extensible Markup Language extensions, typically used to represent, transmit, and store data
2) XML and JSON currently use a wide range of two network transmission data format, respectively:
1, JSON: The market accounted for 90%, lightweight representation of data
2, XML: 10%, indicating that the data is more complex
3) XML Three methods of data parsing:
1. DOM parsing: Load the entire XML data into memory, construct an object, and extract the data from the root node at the first level of parsing. Cons: If the data is large,
Compared to memory, parsing is time consuming. Pros: You can modify XML data
2, Sax parsing: Based on event-driven parsing, the process of reading XML data is the process of parsing, when reading to a key item (start tag ...) )
can be processed. Advantages: Fast resolution. Cons: Cannot modify XML data
3. Use third-party gdata to parse XML data:
1), Import third-party (GData) files
2), Gdata class using the underlying LIBXML2 library file, you need to set the search path of the header file: In the Build settings select the header search Paths, double-click,
Click + to add/USR/INCLUDE/LIBXML2
3), add the binary library file: Click Link binary with Libraries in build phases, click +, select Libxml2.dylib
4) Parse the XML data instance code using Gdata :
1 #import "GDataXMLNode.h"2 intMainintargcConst Char*argv[]) {3 @autoreleasepool {4 //1. Reading XML data5NSData *data=[nsdata Datawithcontentsoffile:@"/users/shared/ftp/book.txt"];
6 //to load XML data into memory to create a Gdataxmldocument object7Gdataxmldocument *doc=[[Gdataxmldocument alloc]initwithdata:data Error:nil];
8 //Get root Tag9Gdataxmlelement *root=[Doc RootElement];
Ten //gets a child tag in the markup based on the name of the child tag (returns an array because there is only one books child tag) OneGdataxmlelement *bookselement=[[root Elementsforname:@"Books"] Firstobject];
A //Get all book tags (arrays) under Books -Nsarray *bookarray=[bookselement Elementsforname:@" Book"];
- //traverse each book tag the for(Gdataxmlelement *bookinchBookarray) { -Gdataxmlelement *nameelement=[book Elementsforname:@"name"][0];
- //StringValue Gets the content string of the tag -NSLog (@"%@", [nameelement stringvalue]);
+ //Get the Auther sub-tag under the book Tag -Gdataxmlelement *autherelement=[book Elementsforname:@"Auther"][0];
+ //gets the child tag under the auther tag name (only one) AGdataxmlelement *authernameelement=[autherelement Elementsforname:@"name"][0]; atNSLog (@"Auther name:%@", [authernameelement stringvalue]);
- //Gets the ID Property object of Book -Gdataxmlnode *idnode=[book Attributeforname:@"ID"]; - //[Idnode StringValue] Take property value -NSLog (@"id:%@", [Idnode stringvalue]); - } in } - return 0; to}
5) XPath expression: string expression to quickly find a tag
1,/: To start parsing from the root tag
2,//: Indicates any position of the mark, satisfies the condition
3,./: Indicates the current tag
4, @: Gets the attributes of the tag
6) Parsing XML data instance code using XPath
1NSData *data=[NSData Datawithcontentsoffile:path];2 //1. Gdataxmldocument Object3Gdataxmldocument *doc=[[Gdataxmldocument alloc]initwithdata:data Error:nil];4 //2. Get the root tag5Gdataxmlelement *root=[Doc RootElement];6 7 //returns all the book tags under any tag8Nsarray *bookarray=[root Nodesforxpath:@"//book"Error:nil];9 Ten //iterate through all the book tags, each of which is described by the books tag and need to be taken out of the encapsulated object One for(Gdataxmlelement *bookinchBookarray) { ABookitem *item=[[Bookitem alloc]init]; - - //@ "./name": Returns the name tag under the current (book) tag theGdataxmlelement *nameelement=[book Nodesforxpath:@"./name"error:nil][0]; -Item.name=Nameelement.stringvalue; - - //take the price tag under the book Tag +Gdataxmlelement *priceelement=[book Elementsforname:@" Price"][0]; -Item.price=[Priceelement stringvalue]; + A //take the name tag under the auther tag under the current (book) tag atGdataxmlelement *authernameelement=[book Nodesforxpath:@"./auther/name"error:nil][0]; -Item.authername=[Authernameelement stringvalue]; - - //Gets the ID Property object of the book node, taking the value -Gdataxmlnode *idnode=[book Attributeforname:@"ID"]; -Item.id=[Idnode stringvalue]; in - //Language Property object that takes the current (book) tag toGdataxmlnode *languagenode=[book Nodesforxpath:@"./@language"error:nil][0]; +Item.language=[Languagenode stringvalue]; - [_bookarray Addobject:item]; the } * $Nsarray *namearray=[root Nodesforxpath:@"//auther/name"Error:nil];Panax Notoginseng for(Gdataxmlelement *nameinchNameArray) { -NSLog (@"name:%@", [name StringValue]); the } +NSLog (@"**********"); A the //gets the book tag under any tag, and the price string value of the child tag in book >100 +Gdataxmlelement *book2=[root Nodesforxpath:@"//book[price>100]"error:nil][0]; -NSLog (@"%@", [Book2 stringvalue]);
IOS Stage Learning 23rd Day notes (XML Data format Introduction)