gpt4 book ai didi

iphone - 在 iPhone 中使用 touch xml 解析文件

转载 作者:行者123 更新时间:2023-12-03 20:28:29 26 4
gpt4 key购买 nike

<ArrayOfBar xmlns="http://schemas.datacontract.org/2004/07/BarometerModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Bar>
<BarId>1</BarId>
<BarType>Pub</BarType>
<Location>West Ocean City</Location>
<LocationId>1</LocationId>
<Name>Mickey Fine Bar & Grill</Name>
<Phone>9884020844</Phone>
<Rating>3.50</Rating>
<Status>Open</Status>
<TypeId>1</TypeId>
<Website>www.mickeyfinebar.com</Website>
</Bar>
</ArrayOfBar>`

如何使用 touch XML 解析此文件?

这是我正在使用的代码:

-(void) grabRSSFeed:(NSString *)blogAddress {

// Initialize the blogEntries MutableArray that we declared in the header
blogEntries = [[NSMutableArray alloc] init];

// Convert the supplied URL string into a usable URL object
NSURL *url = [NSURL URLWithString: blogAddress];

// Create a new rssParser object based on the TouchXML "CXMLDocument" class, this is the
// object that actually grabs and processes the RSS data
CXMLDocument *rssParser = [[[CXMLDocument alloc] initWithContentsOfURL:url options:0 error:nil] autorelease];

// Create a new Array object to be used with the looping of the results from the rssParser
NSArray *resultNodes = NULL;

// Set the resultNodes Array to contain an object for every instance of an node in our RSS feed
resultNodes = [rssParser nodesForXPath:@"//Bar" error:nil];

// Loop through the resultNodes to access each items actual data
for (CXMLElement *resultElement in resultNodes) {

// Create a temporary MutableDictionary to store the items fields in, which will eventually end up in blogEntries
NSMutableDictionary *blogItem = [[NSMutableDictionary alloc] init];

// Create a counter variable as type "int"
int counter;

// Loop through the children of the current node
for(counter = 0; counter < [resultElement childCount]; counter++) {

// Add each field to the blogItem Dictionary with the node name as key and node value as the value
[blogItem setObject:[[resultElement childAtIndex:counter] stringValue] forKey:[[resultElement childAtIndex:counter] name]];
}

// Add the blogItem to the global blogEntries Array so that the view can access it.
[blogEntries addObject:[blogItem copy]];
}

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [blogEntries count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}

// Set up the cell
int blogEntryIndex = [indexPath indexAtPosition: [indexPath length] -1];
cell.textLabel.text=[[blogEntries objectAtIndex: blogEntryIndex] objectForKey: @"BarId"];
return cell;
}

谁能帮我解析一下吗?

最佳答案

CXMLDocument *rssParser = [[[CXMLDocument alloc] initWithContentsOfURL:url options:0  error:nil]autorelease];

NSArray *items = [rssParser nodesForXPath:@"//Bar" error:nil];
for (CXMLElement *resultElement in items) {
Data *objData = [[Data alloc] init];

NSArray *BarIds = [resultElement elementsForName:@"BarId"];
for (CXMLElement *id in BarIds) {
objData.id = id.stringValue;
break;
}
NSArray *Types = [resultElement elementsForName:@"Type"];
for (CXMLElement *Type in Types) {
objData.type = Type.stringValue;
break;
}
NSArray *Locations = [resultElement elementsForName:@"Location"];
for (CXMLElement *Location in Locations) {
objData.type = Location.stringValue;
break;
}...

使用此功能,您可以使用 TuochXML 从 XML 文件中获取数据,然后将该objData 保存为数组对象...并使用它在表中表示。

可能这对你有用

关于iphone - 在 iPhone 中使用 touch xml 解析文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6978561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com