我已经使用教程中的 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 文件......