10

I've got a XML file which contains some data I would like to use:

<?xml version="1.0" encoding="UTF-8" ?>

<items>
<item name="product" price="19.95" where="store">
This is the first product.
</item>

<item name="product2" price="39.95" where="online">
This is the second product.
</item>

<item name="product3" price="99.95" where="garagesale">
This is the third product.
</item>

</items>

If I made 4 arrays, one for the name, one for the price, one for where it was bought and one for its description, how would I get the data into the arrays?

I figured using NSXMLParser, but couldn't get name, price, where or the description.

I'm stuck on how to do this.

Any help appreciated.

Jack Greenhill
  • 10,240
  • 12
  • 38
  • 70
  • 1
    Have you read [the guide](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/XMLParsing/Articles/HandlingElements.html)? – Georg Fritzsche May 21 '11 at 12:17
  • Possible duplicate of [Best approach for XML parsing on the iPhone](http://stackoverflow.com/questions/842292/best-approach-for-xml-parsing-on-the-iphone) or [Simple XML Parsing](http://stackoverflow.com/questions/3395237/simple-xml-parsing) or [Parsing XML Code on iPhone](http://stackoverflow.com/questions/3616447/parsing-xml-code-on-iphone-sdk) or [Parsing XML in Cocoa](http://stackoverflow.com/questions/1089737/parsing-xml-in-cocoa) or [NSXMLParser on iPhone how do I use it?](http://stackoverflow.com/questions/2964503/). Take your pick. – jscs May 21 '11 at 17:06
  • This is a good tutorial,but One thing I didn't get help. Can you tell me please that how I can read string "This is the first product". – Khalid Usman Apr 08 '12 at 15:53

4 Answers4

15

First you need to create an object that does the parsing. It will instatiate the NSXMLParser instance, set itself as the delegate for the parser and then call the parse message. It can also be responsible for storing your four result arrays:

NSXMLParser * parser = [[NSXMLParser alloc] initWithData:_data];
[parser setDelegate:self];
BOOL result = [parser parse];

The message you are most interested in implementing in your delegate objects is didStartElement. This guy gets called for each element in your XML file. In this callback you can add your name, price & where attributes to their respective arrays.

- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qualifiedName
    attributes:(NSDictionary *)attributeDict
{
    // just do this for item elements
    if(![elementName isEqual:@"item"])
        return;

    // then you just need to grab each of your attributes
    NSString * name = [attributeDict objectForKey:@"name"];

    // ... get the other attributes

    // when we have our various attributes, you can add them to your arrays
    [m_NameArray addObject:name];

    // ... same for the other arrays
}
RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
5

To get the value between the tags (e.g. "This is the first product.") you can override - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

Craig Miller
  • 543
  • 8
  • 9
3

in the follwing method

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {

    if([elementName isEqualToString:@"item"]) {

        NSString *name=[attributeDict objectForKey:@"name"];
        NSString *price=[attributeDict objectForKey:@"price"];
        NSString *where=[attributeDict objectForKey:@"where"];
    }
}
EmptyStack
  • 51,274
  • 23
  • 147
  • 178
SriPriya
  • 1,240
  • 15
  • 22
0

you have to consider the dictionary of item tag as an array and three tag (name,price and where)as the object at index 0,1,2

NIKHIL
  • 2,719
  • 1
  • 26
  • 50