gpt4 book ai didi

iphone - 从这个示例代码的服务器中的 xml 文件解析 xml 值

转载 作者:行者123 更新时间:2023-11-28 19:17:14 25 4
gpt4 key购买 nike

我已经使用教程中的 xml 解析器创建了一个应用程序。但发现本教程很简单。在此 xml 文件是从本地 xml 解析的。但是如果我需要解析 xml 文件中的值,请说 www. xxxxx.com/xxx.xml..我该如何修改代码...请指导..

- (void) applicationDidFinishLaunching:(UIApplication *)application {

// find "sample.xml" in our bundle resources
NSString *sampleXML = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"xml"];
NSData *data = [NSData dataWithContentsOfFile:sampleXML];

// create a new SMXMLDocument with the contents of sample.xml
NSError *error;
SMXMLDocument *document = [SMXMLDocument documentWithData:data error:&error];

// check for errors
if (error) {
NSLog(@"Error while parsing the document: %@", error);
return;
}

// demonstrate -description of document/element classes
NSLog(@"Document:\n %@", document);

// Pull out the <books> node
SMXMLElement *books = [document.root childNamed:@"books"];

// Look through <books> children of type <book>
for (SMXMLElement *book in [books childrenNamed:@"book"]) {

// demonstrate common cases of extracting XML data
NSString *isbn = [book attributeNamed:@"isbn"]; // XML attribute
NSString *title = [book valueWithPath:@"title"]; // child node value
float price = [[book valueWithPath:@"price"] floatValue]; // child node value (converted)

// show off some KVC magic
NSArray *authors = [[book childNamed:@"authors"].children valueForKey:@"value"];

NSLog(@"Found a book!\n ISBN: %@ \n Title: %@ \n Price: %f \n Authors: %@", isbn, title, price, authors);
}
}

我已经尝试了很多教程很多个小时,最后我在我的应用程序中使用这些代码得到了很好的结果。但是我需要从服务器导入 xml 文件...

最佳答案

显然,我不会编写您需要的所有代码(服务器访问 + XML 解析器 + 协议(protocol),至少要有一个体面的架构),这不是博客。所以:

  1. 首先从服务器下载 XML 文件。您可以使用 NSURLConnection 或者您可以使用第 3 方库(例如 MKNetworKit)。
  2. 从服务器获取数据 (NSData) 后,您可以用它填充解析器。

简而言之就是这样。只是一个指针:

  1. 请不要将您的 XML 解析器放在 applicationDidFinishLaunching: 中,它没有任何意义。如果您需要在 applicationDidFinishLaunching: 时使用它,它是完全有效的,请为 XML 解析器创建一个适当的类。

关于iphone - 从这个示例代码的服务器中的 xml 文件解析 xml 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11324196/

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