Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在此示例代码的服务器中解析xml文件中的xml值_Iphone_Ios_Xcode_Nsurlconnection_Nsxmlparser - Fatal编程技术网

Iphone 在此示例代码的服务器中解析xml文件中的xml值

Iphone 在此示例代码的服务器中解析xml文件中的xml值,iphone,ios,xcode,nsurlconnection,nsxmlparser,Iphone,Ios,Xcode,Nsurlconnection,Nsxmlparser,我已经使用教程中的xml解析器创建了一个应用程序。但是本教程很简单。在本教程中,xml文件是从本地xml解析的。但是如果我需要从xml文件解析值,请访问www.xxxxx.com/xxx.xml。我如何修改代码…请提供指导 - (void) applicationDidFinishLaunching:(UIApplication *)application { // find "sample.xml" in our bundle resources NSString *sampleXML =

我已经使用教程中的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);
}
 }
-(void)applicationdFinishLaunching:(UIApplication*)应用程序{
//在我们的捆绑资源中找到“sample.xml”
NSString*sampleXML=[[NSBundle mainBundle]pathForResource:@“sample”类型:@“xml”];
NSData*data=[NSData data WITH CONTENTS OFFILE:sampleXML];
//创建一个包含sample.xml内容的新SMXMLDocument
n错误*错误;
SMXMLDocument*document=[SMXMLDocumentDocumentWithData:数据错误:&error];
//检查错误
如果(错误){
NSLog(@“解析文档时出错:%@”,错误);
返回;
}
//演示-文档/元素类的描述
NSLog(@“文档:\n%@”,文档);
//拉出节点
SMXMLElement*books=[document.root childNamed:@“books”];
//仔细检查这类儿童
对于(SMXMLElement*book in[books ChildrenName:@“book”]){
//演示提取XML数据的常见情况
NSString*isbn=[book AttributeName:@“isbn”];//XML属性
NSString*title=[book valueWithPath:@“title”];//子节点值
浮动价格=[[book valueWithPath:@“price”]floatValue];//子节点值(已转换)
//展示一些KVC的魔力
NSArray*authors=[[book ChildName:@“authors”]。children valueForKey:@“value”];
NSLog(@“找到一本书!\n ISBN:%@\n标题:%@\n价格:%f\n作者:%@”,ISBN,标题,价格,作者);
}
}

我花了这么多小时尝试了许多教程,最后在我的应用程序中使用这些代码,我感觉很好。但我需要从服务器导入xml文件…

显然,我不会编写您需要的所有代码(服务器访问+xml解析器+协议,至少要有一个像样的体系结构),这不是一个博客。因此:

  • 首先从服务器下载XML文件。您可以使用
    NSURLConnection
    ,也可以使用第三方库(例如)
  • 从服务器获取数据(
    NSData
    )后,可以用它填充解析器
  • 简而言之,就是这样。只是一个指针:

  • 请不要将XML解析器放在
    applicationdFinishLaunching:
    中,这没有任何意义。如果您需要在
    ApplicationIDFinishLaunching:
    启动时使用它,那么它是完全有效的,请为XML解析器创建一个适当的类

  • 显然,我不会编写您需要的所有代码(服务器访问+XML解析器+协议,至少要有一个像样的体系结构),这不是一个博客。因此:

  • 首先从服务器下载XML文件。您可以使用
    NSURLConnection
    ,也可以使用第三方库(例如)
  • 从服务器获取数据(
    NSData
    )后,可以用它填充解析器
  • 简而言之,就是这样。只是一个指针:

  • 请不要将XML解析器放在
    applicationdFinishLaunching:
    中,这没有任何意义。如果您需要在
    ApplicationIDFinishLaunching:
    启动时使用它,那么它是完全有效的,请为XML解析器创建一个适当的类

  • 嗯,它可以简化为两件事:获取XML字符串并对其进行解析。为了获得xml,您只需编写如下内容:

    NSError*error=nil;
    NSString*xmlString=[NSString stringWithContentsOfURL:[NSURL urlWithString:@“www.xxxxx.com/xxx.xml”]编码:NSUTF8STRING编码错误:&错误]

    然后,将该字符串提供给解析器。据我所知,TBXML是最快的,而且很容易使用。你可以从我这里得到它

    之后,您需要遍历XML的节点,这很容易,您可以找到许多示例。一些文件


    干杯

    好吧,它可以简化为两件事:获取XML字符串并对其进行解析。为了获得xml,您只需编写如下内容:

    NSError*error=nil;
    NSString*xmlString=[NSString stringWithContentsOfURL:[NSURL urlWithString:@“www.xxxxx.com/xxx.xml”]编码:NSUTF8STRING编码错误:&错误]

    然后,将该字符串提供给解析器。据我所知,TBXML是最快的,而且很容易使用。你可以从我这里得到它

    之后,您需要遍历XML的节点,这很容易,您可以找到许多示例。一些文件


    干杯

    我不是在问代码,我知道这不是为了这个…我只是问我需要做什么架构或基本的基础工作…tks是为了你的答案…“我如何修改代码…请提供指导…”已经给出了指导。如果你需要进一步的帮助,请告诉我Dany。对不起,如果我太苛刻了,stackoverflow让我这么做。我不是在问代码,我知道这样做不是为了这个…我只是问我需要做什么架构或基本的基础工作…tks是为了你的答案…“我如何修改代码…请提供指导…”已经给出了指导。如果你需要进一步的帮助,请告诉我Dany。对不起,如果我太苛刻了,stackoverflow让我这么做了。你附加的TBXML链接只处理静态xml文件。你能告诉我关于动态文件的情况吗?你附加的TBXML链接只处理静态xml文件。你能告诉我关于动态文件的情况吗。。。