XML:通过内部节点嵌套节点解析和部分
分离我有下面的XML架构我试图遍历:XML:通过内部节点嵌套节点解析和部分
<routes>
<route>
<group>Group Name - Will be iPhone section name</group>
<item>
<name>Route Name - Will be iphone cell lable</name>
<url>Route URL - View Did select lable</url>
</item>
<item>
<name>Route Name - Will be iphone cell lable</name>
<url>Route URL - View Did select lable</url>
</item>
<item>
<name>Route Name - Will be iphone cell lable</name>
<url>Route URL - View Did select lable</url>
</item>
<item>
<name>Route Name - Will be iphone cell lable</name>
<url>Route URL - View Did select lable</url>
</item>
</route>
<route>
<group>Group 2 - Will be iPhone section name</group>
<item>
<name>Route Name - Will be iphone cell lable</name>
<url>Route URL - View Did select lable</url>
</item>
<item>
<name>Route Name - Will be iphone cell lable</name>
<url>Route URL - View Did select lable</url>
</item>
</route>
</routes>
现在大部分都在项目相当多的项目 - 和Group节点将是该部分的标题为UITableView的
继承人如何我解析(错误地)的XML的时刻:
(这里是xNode_ declaraions :)
static NSString *xNode_route [email protected]"//route";
static NSString *xNode_group [email protected]"group";
static NSString *xNode_item= @"//item";
static NSString *xNode_name = @"name";
static NSString *xNode_url = @"url";
下面是解析的代码:
CXMLDocument *doc = [[CXMLDocument alloc] initWithData:dataReply options:0 error:nil];
NSArray *route = [doc nodesForXPath:xNode_route error:nil];//Root node
for (CXMLElement *item in route)
{
AssessObject *newobj2 = [[AssessObject alloc] init];
NSArray *arryGroup = [item elementsForName:xNode_group];
for(CXMLElement *assGroup in arryGroup)
{
newobj2.assGroup = assGroup.stringValue;
NSLog(@"Assessment Group: %@", assGroup.stringValue);
}
[groupArray addObject:newobj2];
}
NSArray *items = [doc nodesForXPath:xNode_item error:nil];//Root node
for (CXMLElement *item in items)
{
AssessObject *newobj = [[AssessObject alloc] init];
NSArray *arryName = [item elementsForName:xNode_name];
for(CXMLElement *assName in arryName)
{
newobj.assName = assName.stringValue;
NSLog(@"Assessment Name: %@", assName.stringValue);
}
NSArray *arryUrl = [item elementsForName:xNode_url];
for(CXMLElement *assUrl in arryUrl)
{
newobj.assUrl = assUrl.stringValue;
NSLog(@"Assessment URL: %@", assUrl.stringValue);
}
[totalArray addObject:newobj];
}
}
在它要通过所有的项目X有组的量的时刻,所以某处循环不正确。所以如果有6个组,它会显示6次重复的项目。
所以这是第一部分,第二部分实际上是如何将阵列分成组并将正确的项目放入组中?
首先,您没有正确释放内存。
在发布的代码中有6个发布语句 - 全部删除它们。
然后,你需要添加两个声明:
[release newObj2];
为您明确分配对象
:newObj和newObj2,你将它们添加到阵列之后。
然后,如果事情不起作用,我们可以谈论解析。
编辑: 好吧,让我们来谈谈解析:
+ (NSMutableArray *) parseResponseString:(NSString *) responseString {
NSError *error;
CXMLDocument *doc = [[CXMLDocument alloc] initWithXMLString: responseString
options: 0
error: &error];
if (error) {
NSLog(@"CXMLDocument error: %@", [error description]);
[doc release];
return nil;
}
CXMLElement * el = [doc rootElement];
NSMutableArray *theRoutes = [NSMutableArray arrayWithCapacity:128]; // adjust to your needs
if ([[el name] isEqualToString:@"routes"]) { // parameterize these string constants
for (CXMLElement *childElement in [el children]) {
if ([[childElement name] isEqualToString:@"route"]) {
Route *aRoute = [[Route alloc] init];
for (CXMLElement *grandChild in [childElement children]) {
if ([[grandChild name] isEqualToString:@"group"]) {
aRoute.group = [grandChild stringValue];
} else if ([[grandChild name] isEqualToString:@"item"]) {
RouteItem *aRouteItem = [[RouteItem alloc] init];
for (CXMLElement *subItem in [grandChild children]) {
if ([[subItem name] isEqualToString:@"name"]) {
aRouteItem.name = [subItem stringValue];
} else if ([[subItem name] isEqualToString:@"url"]) {
aRouteItem.url = [subItem stringValue];
}
}
if (aRouteItem && aRouteItem.name && aRouteItem.url) {
[aRoute.routeItems addObject:aRouteItem];
}
[aRouteItem release];
}
}
if (aRoute && aRoute.group && aRoute.items.count) {
[theRoutes addObject:aRoute];
}
[aRoute release];
}
}
}
[doc release];
return theRoutes; // this is your array for your tableview data source.
}
嘿 - 谢谢,我修复了这个问题并添加了版本 - 我的解析仍然是重复的,尽管 – TMB87
@Tom - 不知道是什么原因导致了这个问题,但是为了解决问题2,你需要以不同的方式进行解析。不要将XML glob视为不透明的数据库并进行查询,只需从上到下进行解析,随时随地构建数据结构。创建这些对象:Route和RouteItem。 Route有一个组成员(NSString *)和一个RouteItem数组。 RouteItem拥有名称和Url的成员。然后将这个数组提供给你的tableView,cellForRowAtIndexPath可以把它分开来显示你想要的。看到上面的代码(我不应该为你写代码,但你的跳舞网站让我感到好笑)。 – Rayfleck
哪个解析器使用乌尔...我知道通过的NSXMLParser ... – mAc
我使用CXML解析器 – TMB87