Parsing xml NSData for iPhone Development

Source: Internet
Author: User

The xml string is:

<Users>
<User name = "hoge" age = "20"/>
<User name = "fuga" age = "30"/>
</Users>

 

// Parse button event

-(IBAction) btnXml

{

NSString * str = [uitextview1 text];

// Convert string to data

NSData * xmlData = [str dataUsingEncoding: NSUTF8StringEncoding];

// NSXMLParser class is required for xml parsing. First, an NSXMLParser object is declared.

// Start Parsing

NSXMLParser * xmlRead = [[NSXMLParser alloc] initWithData: xmlData]; // initialize the NSXMLParser object

[XmlRead setDelegate: self]; // sets the DNS proxy for the NSXMLParser object

[XmlRead parse]; // call the proxy to parse the NSXMLParser object and check whether the parsing is successful.

}

 

// Parser to read content from two nodes

-(Void) parser :( NSXMLParser *) parser foundCharacters :( NSString *) string {

// NSLog (@ "% @", string );}

 

 

// Obtain the end value of the node

-(Void) parser :( NSXMLParser *) parser didEndElement :( NSString *) elementName

NamespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName {

// NSLog (@ "% @", elementName );}

 

// Get the nod Value

-(Void) parser :( NSXMLParser *) parser didStartElement :( NSString *) elementName

NamespaceURI :( NSString *) namespaceURI qualifiedName :( NSString *) qName

Attributes :( NSDictionary *) attributeDict

{

// NSLog (@ "% @", elementName );

If ([elementName isEqualToString: @ "user"])

{

NSString * strName = [attributeDict valueForKey: @ "name"];

NSString * strAge = [attributeDict valueForKey: @ "age"];

NSString * strUser = uitextview2.text;

StrUser = [strUser stringByAppendingFormat: @ "Name: % @, age: % @ \ n", strName, strAge];

[Uitextview2 setText: strUser];

// NSLog (@ "Name: % @, age: % @", strName, strAge );

}

}

 

From fanwa Software

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.