XML parsing code for iOS development
1. Load and parse XML file NSString *path = [[NSBundle mainbundle] pathforresource:@ "Xml.txt" oftype:nil]; NSData *data = [[NSData alloc] initwithcontentsoffile:path]; Gdataxmldocument represents an XML Document Object//Initwithdata using NSData initialization, which is parsing gdataxmldocument *doc = [[Gdataxmldocument alloc] In Itwithdata:data options:0 Error:nil]; 2. Get the specified node XPath//cityname path:/root/systemconfig/cityname nsarray *array = [Doc nodesforxpath:@ "/root/systemconfig/ CityName "Error:nil"; CityName gdataxmlelement *element = [array firstobject]; NSLog (@ "name=%@ value=%@", element.name,element.stringvalue); 3. Gets the properties of the specified node Nsarray *items = [Doc nodesforxpath:@ "/root/systemconfig/comechannel/item" error:nil]; Gdataxmlelement *item = [items firstobject]; Gets the property that the property uses Gdataxmlelement to represent for (Gdataxmlelement *attr in item.attributes) {NSLog (@ "a-name=%@ avalue=%@", a Ttr.name,attr.stringvalue); }//4. Gets all nodes of the specified name (regardless of location)//xpath syntax://item nsarray *allitem= [Doc nodesforxpath:@ "//item" error:nil]; For (Gdataxmlelement *e in Allitem) {NSLog (@ "name =%@", e.name); }//5. Gets all properties of the specified name (regardless of which node)//xpath syntax://@value Nsarray *allvalue = [doc nodesforxpath:@ "//@value" error:nil]; For (Gdataxmlelement *e in Allvalue) {NSLog (@ "value =%@", e.stringvalue); }//6. Iterate through the XML file//Get the root node gdataxmlelement *root = doc.rootelement; Get child node//root.children//Get child node number//root.childcount//Get child node of specified name//root elementsforname:<# (NSString *) #& gt;}
:
XML parsing code for iOS development