XML parsing code for iOS development

Source: Internet
Author: User

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

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.