XML topic: Using nsxmlparser to parse XML files

Source: Internet
Author: User

Use nsxmlparser to parse XML files

1. Set the delegate object and start Parsing
Nsxmlparser * parser = [[nsxmlparser alloc] initwithdata: Data]; // you can also use initwithcontentsofurl to directly download the object, but this is not the case for one reason:
// It's also possible to have nsxmlparser download the data, by passing it a URL, but this is not desirable
// Because it gives less control over the network, especially in responding to connection errors.
[Parser setdelegate: Self];
[Parser parse];

2. Common delegate Methods
-(Void) parser :( nsxmlparser *) parser didstartelement :( nsstring *) elementname
Namespaceuri :( nsstring *) namespaceuri
Qualifiedname :( nsstring *) QNAME
Attributes :( nsdictionary *) attributedict;
-(Void) parser :( nsxmlparser *) parser didendelement :( nsstring *) elementname
Namespaceuri :( nsstring *) namespaceuri
Qualifiedname :( nsstring *) QNAME;
-(Void) parser :( nsxmlparser *) parser foundcharacters :( nsstring *) string;
-(Void) parser :( nsxmlparser *) parser parseerroccurred :( nserror *) parseerror;

Static nsstring * feedurlstring = @ "http://www.yifeiyang.net/test/test.xml ";

3. Application Example
-(Void) parsexmlfileaturl :( nsurl *) URL parseerror :( nserror **) Error
{
Nsxmlparser * parser = [[nsxmlparser alloc] initwithcontentsofurl: url];
[Parser setdelegate: Self];
[Parser setshouldprocessnamespaces: No];
[Parser setshouldreportnamespaceprefixes: No];
[Parser setshouldresolveexternalentities: No];
[Parser parse];
Nserror * parseerror = [parser parsererror];
If (parseerror & error ){
* Error = parseerror;
}
[Parser release];
}

-(Void) parser :( nsxmlparser *) parser didstartelement :( nsstring *) elementname namespaceuri :( nsstring *) namespaceuri
Qualifiedname :( nsstring *) QNAME attributes :( nsdictionary *) attributedict {
// Element start handle
If (QNAME ){
Elementname = QNAME;
}
If ([elementname isinclutostring: @ "user"]) {
// Output Attribute Value
Nslog (@ "name is % @, age is % @", [attributedict objectforkey: @ "name"], [attributedict objectforkey: @ "Age"]);
}
}

-(Void) parser :( nsxmlparser *) parser didendelement :( nsstring *) elementname namespaceuri :( nsstring *) namespaceuri
Qualifiedname :( nsstring *) QNAME
{
// End handle of the element
If (QNAME ){
Elementname = QNAME;
}
}

-(Void) parser :( nsxmlparser *) parser foundcharacters :( nsstring *) String
{
// Obtain the text of an element
}
Nserror * parseerror = nil;
[Self parsexmlfileaturl: [nsurl urlwithstring: feedurlstring] parseerror: & parseerror];

 

Http://blog.csdn.net/z251257144/article/details/7175634

XML topic: Using nsxmlparser to parse XML files

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.