我该怎么做? (NSXML解析器)
我对 NSXMLParser 还很陌生,我需要一些建议。这是我的情况:
我正在向服务器发送 SOAP 请求以获取“订单”列表,该列表返回以下格式的订单列表:
<Order>
<OrderID> string </OrderID>
<OrderName> string </OrderName>
</Order>
因此,我使用一堆这些订单解析 xml,然后用可变的 nsarray 填充可变的 nsarray字典,所以最后看起来像这样:
(
{
OrderID = 2011417335319;
OrderNumber = 100;
},
{
OrderID = 2011340029503;
OrderNumber = TestOrder3;
},
{
OrderID = 20113223404613;
OrderNumber = 1234;
},
{
OrderID = 20113692554635;
OrderNumber = EricOrder;
},
{
OrderID = 2;
OrderNumber = TestOrder2;
},
{
OrderID = 201144231410461;
OrderNumber = TestOrder4;
}
)
现在,对于每个订单(基于 OrderID),我需要发送另一个请求,该请求将返回一个单位列表。单元具有属性“单元编号和名称”。现在这就是我困惑的地方。解析订单后,我如何为我得到的每个订单:
- 发送 SOAP 请求
- 收到响应时,创建一个 NSXMLParser
- 并解析它。
我怎样才能动态地做到这一点?解析顺序时,一切都很简单,因为我只需要这样做:
- (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];
}
}
但是现在,将有多个 NSXMLParsers 同时运行,所以我对应该如何执行此操作感到有点困惑。
另外,快速提问。我还有一个请求,返回大约 2,000 个元素以及很多子元素。将其存储在内存中、搜索它然后填充 nstableview 的最佳方法是什么?
谢谢。
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 = EricOrder;
},
{
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当在数据层次结构的各个级别上运行多个 nsxmlparser 时,我会在内存中保留另一个变量来描述当前正在解析的对象。完成对初始数据的解析后,设置变量以指示您已完成第二级解析。然后,在解析方法中,您可以根据“progress”变量的值让 nsxmlparser 表现不同。
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.据我了解你的情况,我要做的就是让每个 NSXMLParser 实例都有它自己的 NSXMLParserDelegate 类型,它可以对解析的元素进行正确的处理。举个例子,我会:
XXOrderListParsingDelegate,有它自己的委托方法(didStartElement、didEndElement、foundCharacters,你拥有的那些)
当您创建 NSXMLParser 时,为其分配给定情况的正确委托。
这些代表应该能够访问您的模型(字典、部分等),这可能需要对您的设计进行一些更改。
这应该是正确的。
关于你的第二个问题,一种可能性是使用 sqlite。 这里有一个教程。
1 http://blog.objectgraph.com/index.php/2010/04/08/how-to-use-sqlite-with-uitableview-in-iphone-sdk/
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/
试试这个
给你一个简单的问题...你可以选择 Coredata Concepts。简单又有趣。有很多很好的教程可用。
Try this out
For you Quick question... You can go for Coredata Concepts. Easy and interesting. There are many good tutorials available.