<content name="Bikes" last_updated_time="">
<bike name="MGP30" image_url="Mobile_App/images/mgp30_bike_banner.png">
<tspecs>
<image url="images/our-bikes/mgp30.png"/>
<image url="images/our-bikes/mgp30_tech_spec/1.jpg"/>
<image url="images/our-bikes/mgp30_tech_spec/2.jpg"/>
<image url="images/our-bikes/mgp30_tech_spec/3.jpg"/>
<image url="images/our-bikes/mgp30_tech_spec/4.jpg"/>
<image url="images/our-bikes/mgp30_tech_spec/5.jpg"/>
<image url="images/our-bikes/mgp30_tech_spec/6.jpg"/>
<image url="images/our-bikes/mgp30_tech_spec/7.jpg"/>
<spec>
<name>Engine Type</name>
<value>4 strokes liquid cooled DOHC</value>
</spec>
<spec></spec>
<spec></spec>
<spec></spec>
<spec></spec>
<spec></spec>
<spec></spec>
<spec></spec>
<spec></spec>
<spec></spec>
<spec></spec>
<spec></spec>
<spec></spec>
<spec></spec>
</tspecs>
<photos></photos>
<workshop></workshop>
</bike>
<bike name="GP125" image_url="Mobile_App/images/gp125_bike_banner.png"></bike>
</content>
This is the first time I am working with xml parsing using core data.I have the above xml data which i receive from the server. I am not bale to understand how to create the relationships between the entities. How do I parse it and keep storing it using core data.
Content 是根对象,以 Bikes 作为子元素。每个自行车元素都有技术规范、照片和车间数据。每个 tspec 都有一组图像和一组规范数据。每个规范数据都有一个名称和值。
最佳答案
在 iOS 中解析 XML 是一个艰苦的过程。使用GDataXML将xml解析为数组格式对应的字典如下。
#define kBikeName @"name"
#define kBikeImageURL @"image_url"
#define kBikeImages @"image"
#define kURL @"url"
#define kBikeSpecs @"spec"
#define kBikeSpecName @"name"
#define kBikeSpecValue @"value"
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Bikes" ofType:@"xml"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
GDataXMLDocument *document = [[GDataXMLDocument alloc]initWithData:fileData
encoding:NSUTF8StringEncoding
error:nil];
NSArray *tempBikes = [document nodesForXPath:@"//content/bike" error:nil];
NSMutableArray *bikes = [NSMutableArray array];
for (GDataXMLElement *element in tempBikes) {
NSMutableDictionary *bike = [NSMutableDictionary dictionary];
NSString *bikeName = [[element attributeForName:kBikeName] stringValue];
bike[kBikeName] = bikeName;
NSString *imageURL = [[element attributeForName:kBikeImageURL] stringValue];
bike[kBikeImageURL] = imageURL;
NSArray *tempImages = [element nodesForXPath:@"tspecs/image" error:nil];//elementsForName:@"image"];
NSMutableArray *images = [NSMutableArray array];
for (GDataXMLElement *imageElement in tempImages) {
NSString *url = [[imageElement attributeForName:kURL]stringValue];
if (url) {
[images addObject:url];
}
}
if (images) {
bike[@"Images"] = images;
}
NSArray *tempSpecs = [element nodesForXPath:@"tspecs/spec" error:nil];//elementsForName:@"spec"];
NSMutableArray *specs = [NSMutableArray array];
for (GDataXMLElement *specElement in tempSpecs) {
NSMutableDictionary *specDict = [NSMutableDictionary dictionary];
NSString *specName = [[specElement elementsForName:kBikeSpecName][0]stringValue];
if (specName) {
specDict[kBikeSpecName] = specName;
}
NSString *specValue = [[specElement elementsForName:kBikeSpecValue][0]stringValue];
if (specValue) {
specDict[kBikeSpecValue] = specValue;
}
if ([[specDict allKeys] count]) {
[specs addObject:specDict];
}
}
if (specs) {
bike[@"Specs"] = specs;
}
[bikes addObject:bike];
}
NSLog(@"%@",bikes);
现在您有了字典对象数组。您可能已经形成了 NSManagedObject
仪式。但如果您将来想使用 JSON,这将提供更多可重用的代码。
您应该至少拥有三个实体。如果您愿意,您还可以创建自行车技术规范,它将与自行车建立一对一的关系,并与图像和规范建立一对多的关系。但这似乎没有必要。
- 自行车(与自行车图像和规范的一对多关系)
- 自行车图片(与自行车的一对一关系)
- 自行车规范(与自行车的一对一关系)
第三方库MagicalRecord
提供无代码数据导入。但它要求您的实体拥有唯一的 ID。
例如:如果有一个实体Bike
,它应该有一个bikeID
。 BikeImage
应具有 bikeImageID
,BikeSpec
应具有 bikeSpecID
。由于您没有这些,因此无法使用。但我这样说就好像您决定更改结构一样,可以将这些内容包含在内以利用这个很棒的功能。
设置实体之间的正关系和逆关系。枚举已解析的数组。执行以下步骤
- 创建一个新的自行车对象,设置其值
- 创建图像实例,将之前创建的自行车对象设置为其父对象。仅需要一侧。另一面则通过核心数据来解决。
- 对规范也重复相同的步骤 2。
- 保存上下文。
这不限制重复条目的创建。
关于ios - 使用iOS核心数据解析xml数据并存储它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17022422/