How should I do this? (NSXMLParser)
I am pretty new to NSXMLParser and I need some advice. Here's my situation:
I am sending SOAP request to a server to get a list of "Orders" which return a list of orders in this format:
<Order>
<OrderID> string </OrderID>
<OrderName> string </OrderName>
</Order>
So, I parse the xml with bunch of these Orders, and I populate my mutable nsarray with mutable dictionaries, so in the end it looks like this:
(
{
OrderID = 2011417335319;
OrderNumber = 100;
},
{
OrderID = 2011340029503;
OrderNumber = TestOrder3;
},
{
OrderID = 20113223404613;
OrderNumber = 1234;
},
{
OrderID = 20113692554635;
OrderNumber = Eri开发者_开发问答cOrder;
},
{
OrderID = 2;
OrderNumber = TestOrder2;
},
{
OrderID = 201144231410461;
OrderNumber = TestOrder4;
}
)
Now, for each of these Orders (base on the OrderID), I need to send another request, which will return a list of Units. Units have property "Unit Number and Name". Now this is where I am confused. After parsing Order, how do I, for each of the Orders I got:
- Send a SOAP request
- When received response, create an NSXMLParser
- and then parse it.
How can I do that dynamically? When parsing order, everything was simple as I just had to do this:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"GetOrdersResult"]) {
if (array) {
[array release];
}
array = [[NSMutableArray alloc] init];
} else if ([elementName isEqualToString:@"Order"]) {
if (dict) {
[dict release];
}
dict = [[NSMutableDictionary alloc] initWithCapacity:2];
} else if ([elementName isEqualToString:@"OrderID"]) {
sections = E_OrderID;
} else if ([elementName isEqualToString:@"OrderNumber"]) {
sections = E_OrderNumber;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (sections == E_OrderID) {
[dict setObject:string forKey:@"OrderID"];
} else {
[dict setObject:string forKey:@"OrderNumber"];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"Order"]) {
[array addObject:dict];
}
}
But now, there's going to be multiple NSXMLParsers running at the same time, so I am a bit confused as how I should do this.
Also, quick question. I also have a request that return around 2,000 elements with alot of sub elements. What's the best way to store that in memory, search through it, and then populate an nstableview?
Thanks.
When running multiple nsxmlparser
s on various levels of data hierarchy, I would keep another variable in memory which describes the current object being parsed. When you finish parsing the initial data, set the variable to indicate that you are up to the second level of parsing. Then, in your parsing methods, you can have your nsxmlparser behave differently, based on the value of the "progress" variable.
As far as I understand your case, what I would do is having each NSXMLParser instance have it's own kind of NSXMLParserDelegate that would do the proper processing of the parsed elements. just as an example, I would have:
XXOrderListParsingDelegate, with its own delegates method (didStartElement, didEndElement, foundCharacters, the ones you have)
XXOrderParsingDelegate, with the delegate methods suitable for this case;
When you create an NSXMLParser, assign to it the proper delegate for the given case.
those delegates should have all access to your model (dict, sections, whatever), this could require some change in your design.
This should get it right.
as to your second question, One possibility is using sqlite. here you have a tutorial.
1 http://blog.objectgraph.com/index.php/2010/04/08/how-to-use-sqlite-with-uitableview-in-iphone-sdk/
Try this out
for (NSManagedObject* managedObject in "YourArray") {
//You can get the OrderID's individually like this
[managedObject valueForKey:@"OrderID"];
//With the orderID you can send requests for each
}
For you Quick question... You can go for Coredata Concepts. Easy and interesting. There are many good tutorials available.
精彩评论