Data in XML format is a transmission format for data. Because it is easy to write. The structure is clear, so popular with the program ape, very many people like to use XML format data transmission or as a program configuration information.
I am now in the future to parse XML format data in iOS and use Swift in language.
Start by writing a simple XML yourself:
<data> <person age= ">zhangsan</person> <person age=" 2 ">lisi</person> <person>wangwu</person> <a> <b> <c>many people</c> </b> </a></data>
First write this XML into a file Data.xml stored in supporting files, read in the program.
(1) First in this viewcontroller to achieve a trust: Nsxmlparserdelegate. The XML file is then read in Viewdidload (). The Viewdidload () method is implemented such as the following:
Override Func Viewdidload () { super.viewdidload () //Do any additional setup after loading the view, typically fro M a nib. var parser = Nsxmlparser (Contentsofurl:nsurl (FileURLWithPath:NSBundle.mainBundle (). Pathforresource ("Data", OfType: "XML")!)) parser!. delegate = self parser!. Parse () }
(2) then read the head node value, such as <data>,<person>,<a>,<b>,<c> to implement a parse () method:
Func parser (Parser:nsxmlparser, didstartelement elementname:string, namespaceuri:string?, QualifiedName qname:string ?, attributes Attributedict: [Nsobject:anyobject]) { println (elementname) //Output each header node, such as data. person,person,a,b,c; }
The implementation results such as the following: meet our expectations.
。
(3) Then try to read the attribute value, such as age in XML.
is also implemented in the parse () method described above.
The code is as follows:
Func parser (Parser:nsxmlparser, didstartelement elementname:string, namespaceuri:string?, QualifiedName qname:string ?, Attributes attributedict: [Nsobject:anyobject]) { if elementname = = ' Person ' { if let Age:anyobject = Attribut edict[' age ']{ println ("Age = \ (age)")}}}
The output results are as follows:
。
(4) Finally we will read the value of the node together with the key values included in it.
A parse method needs to be rewritten in the Nsxmlparserdelegate trust. Two of these rewrite the parse method such as the following:
Func parser (Parser:nsxmlparser, didstartelement elementname:string, namespaceuri:string?), QualifiedName qname:string?, attributes Attributedict: [Nsobject:anyobject]) { currentnodename = elementname
} Func parser (parser:nsxmlparser, foundcharacters string:string?)
) { var str = string?. Stringbytrimmingcharactersinset (Nscharacterset.whitespaceandnewlinecharacterset ()) if str! = "" { println ( "Current node= \ (currentnodename), value=\ (str!)") } }
The final output is, for example, the following: compliant/expected results.
。
(5) The code for the entire program, such as the following. There is a more specific gaze in it:
Import Uikitclass Viewcontroller:uiviewcontroller, nsxmlparserdelegate{override func Viewdidload () {Super.vie Wdidload ()//Read the contents from data.xml var parser = Nsxmlparser (contentsofurl:nsurl. Mainbundle (). Pathforresource ("Data", OfType: "xml")!)) parser!. delegate = self//commit with XML parsing. Need to rewrite the method; parser!. Parse ()} var currentnodename:string! Wait to record the name of the node currently being read; Func parser (Parser:nsxmlparser, didstartelement elementname:string, namespaceuri:string?, qua Lifiedname qname:string?, attributes Attributedict: [Nsobject:anyobject]) {println (elementname)//output Each header node, such as data. Person,person,a,b,c; Currentnodename = elementname if elementname = = "Person" {//Read property values under a node. If there is an age attribute under person, there may be some person nodes that do not have an age attribute and need to use if to infer; attributedict[' age ']{println ("Age = \ (age)") }}} Func parser (Parser:nsxmlparser, foundcharacters string:string?) {//In the reading of the entire XML file, the spaces are also read as strings. So you need to delete empty lines and spaces;//string is the key value of this node; var str = string?. Stringbytrimmingcharactersinset (Nscharacterset.whitespaceandnewlinecharacterset ())//also need to infer whether this key value is empty. Because there are no values between the kinsoku nodes, such as <d></d> and so on. If str! = "" {println ("current node= \ (currentnodename), value=\ (str!)")}} Override Func didreceivememorywarning () {super.didreceivememorywarning ()}}
In fact, the most important thing to parse XML is that the client uses SOAP to access the WebService, at which point WebService returns XML very much.
GitHub home: https://github.com/chenyufeng1991. Welcome to visit us!
Parsing XML Format data for iOS development