我有这个带有一些 id 的 XML,每个都有一个数据,我试图解析所有的 XML,所以最后我会得到一个像这样的对象:
A1Object[a1,a1,a1,a1]
A2Object[a2,a2,a2,a2]
问题是首先只解析 A1,A2 ......
xml:
<response>
<tmm>
<tm id="A1"><DocumentElement>
<cell>
<name>Tom</name>
<lastName>LastNameTom</lastName>
</cell>
<cell>
<name>Tom1</name>
<lastName>LastNameTom1</lastName>
</cell>
</tm>
<tm id="A2">
<cell>
<carType>5</carType>
<catId>4545</carId>
</cell>
<cell>
<carType>6</carType>
<catId>565656</carId>
</cell>
</tm>
<tm id="A3">...</tm>
</tmm>
</response>
我对每个对象(a1,a2 ...)都有他与他的领域的干扰
我正在尝试下一件事:
if (xmlDocument != nil)
{
NSArray *nodes = [xmlDocument nodesForXPath:@"//cell" error: nil];
if (nodes != nil)
{
for (CXMLNode *node in nodes)
{
A1 *a1Object = [[A1 alloc] init];
for (int i = 0; i < [node childCount]; i++)
{
CXMLNode *child = [node childAtIndex: i];
NSString *str = [child stringValue];
if ([[child name] isEqualToString: @"name"])
{
a1Object.A1FacilityId = ([str length] > 0) ? str : nil;
}
if ([[child name] isEqualToString: @"lastName"])
{
a1Object.A1FacilityStreetName = ([str length] > 0) ? str : nil;
}
}
[self.A1s addObject:a1Object];
[a1Object release];
}
}
}
我如何才能改进它,所以最后我得到它:
A1Object[a1,a1,a1,a1]
A2Object[a2,a2,a2,a2