0

嘿,任何人都可以请帮帮我。我是 xcode 的新手(目标 c),我正在使用 asp.net Web 服务,我只是将该服务中的 3 个文件名返回到我的 xcode 项目中。现在我想做的是解析生成的 Web 服务数据以 XML 格式转换为 xcode 中的简单字符串。

我的 xml 字符串是

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  <string>Desert.jpg</string>
  <string>Koala.jpg</string>
  <string>Tulips.jpg</string>
</ArrayOfString>

我在 ma xcode 项目中有四个类文件,如下所示

图像保存测试视图控制器.m 文件

//
//  ImageSaveTestViewController.m
//  ImageSaveTest
//
//  Created by Kiichi Takeuchi on 4/4/10.
//  Copyright ObjectGraph LLC 2010. All rights reserved.
//

#import "ImageSaveTestViewController.h"

@implementation ImageSaveTestViewController


- (void)viewDidLoad {
    [super viewDidLoad];





    NSString *urlString = @"http://192.168.3.106/local_storage/Webservice1.asmx/HelloWorld";
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod: @"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    NSError *errorReturned = nil;
    NSURLResponse *theResponse =[[NSURLResponse alloc]init];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];


    if (errorReturned) 
    {
        //...handle the error
    }



        NSString *retVal = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        **NSLog(@"%@", retVal);**
//this is the retval which i want to convert in simple string.its containig that above xml file

        //...do something with the returned value        




    NSLog(@"Downloading...");
    // Get an image from the URL below

    id path = @"http://192.168.3.106/chmall/images/1.jpg";
    NSURL *url1 = [NSURL URLWithString:path];
    NSData *data11=[NSData dataWithContentsOfURL:url1];
    UIImage *img1=[[UIImage alloc] initWithData:data11];    

    //UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"192.168.3.106/chmall/images/1.jpg"]]];

    NSLog(@"%f,%f",img1.size.width,img1.size.height);

    // Let's save the file into Document folder. 
    // You can also change this to your desktop for testing. (e.g. /Users/kiichi/Desktop/)
     NSString *docDir = @"/Users/niketkapadia/Desktop";

    //NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    // If you go to the folder below, you will find those pictures
    NSLog(@"%@",docDir);

    NSLog(@"saving png");
    NSString *pngFilePath = [NSString stringWithFormat:@"%@/test.png",docDir];
    NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(img1)];
    [data1 writeToFile:pngFilePath atomically:YES];

    NSLog(@"saving jpeg");
    NSString *jpegFilePath = [NSString stringWithFormat:@"%@/test.jpeg",docDir];
    NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(img1, 1.0f)];//1.0f = 100% quality
    [data2 writeToFile:jpegFilePath atomically:YES];

    NSLog(@"saving image done");

    [img1 release];
}

@end

appdeleget.m 文件

//  ImageSaveTestAppDelegate.m
//  ImageSaveTest
//
//  Created by Kiichi Takeuchi on 4/4/10.
//  Copyright ObjectGraph LLC 2010. All rights reserved.
//

#import "ImageSaveTestAppDelegate.h"
#import "ImageSaveTestViewController.h"

@implementation ImageSaveTestAppDelegate

@synthesize window;
@synthesize viewController;


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

    // Override point for customization after app launch    
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}


- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}


@end

delagte.h 文件

//
//  ImageSaveTestAppDelegate.h
//  ImageSaveTest
//
//  Created by Kiichi Takeuchi on 4/4/10.
//  Copyright ObjectGraph LLC 2010. All rights reserved.
//

#import <UIKit/UIKit.h>

@class ImageSaveTestViewController;

@interface ImageSaveTestAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    ImageSaveTestViewController *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet ImageSaveTestViewController *viewController;

@end

视图控制器.h

//
//  ImageSaveTestViewController.h
//  ImageSaveTest
//
//  Created by Kiichi Takeuchi on 4/4/10.
//  Copyright ObjectGraph LLC 2010. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ImageSaveTestViewController : UIViewController {

}

@end
4

1 回答 1

1

SudzC能够从 WSDL 文件生成 Objective C 代码。它是一项在线服务,因此您可以上传或链接您的 WSDL 文件,并且您会获得一个带有可运行代码的 zip,以便与您的 Web 服务进行通信。

还有很多标记为“sudzc”的问题。


您还可以使用NSXMLParser读取 XML:

(演示)

#import <Cocoa/Cocoa.h>

@interface AOSParser : NSObject {
    NSMutableString *capturedCharacters;
    BOOL captureCharacters;

    NSString *text;
    NSMutableArray *items;
}

- (NSMutableArray *)parseXMLData:(NSData *)data parseError:(NSError **)error;

// NSXMLParserDelegate methods (NSXMLParser doesn't need an explicit protocol implementation)
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict;
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
@end

@implementation AOSParser

- (NSMutableArray *)parseXMLData:(NSData *)data parseError:(NSError **)error {
    items = [NSMutableArray array];

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    [parser setDelegate:self];
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];

    [parser parse];

    if (error && [parser parserError]) {
        *error = [parser parserError];
    }

    //[parser release]; // Seems to be auto-released

    return [items retain];
}

// Called when a start-tag is found
- (void)parser:(NSXMLParser *)parser
        didStartElement:(NSString *)elementName
        namespaceURI:(NSString *)namespaceURI
        qualifiedName:(NSString *)qualifiedName
        attributes:(NSDictionary *)attributeDict {

    //NSLog(@"<%@>", elementName);

    if ([elementName isEqual:@"string"]) {
        captureCharacters = YES;
        capturedCharacters = [NSMutableString string];
    }

}

// Called when plain text is found
- (void)parser:(NSXMLParser *)parser
        foundCharacters:(NSString *)string {

    //NSLog(@"Text: %@", string);

    if (captureCharacters) {
        [capturedCharacters appendString:string];
    }
}

// Called when an end tag is found
- (void)parser:(NSXMLParser *)parser
        didEndElement:(NSString *)elementName
        namespaceURI:(NSString *)namespaceURI
        qualifiedName:(NSString *)qName {

    //NSLog(@"</%@>", elementName);

    if (captureCharacters) {
        captureCharacters = NO;
        text = [NSString stringWithString:capturedCharacters];
        [capturedCharacters release];
    }

    if ([elementName isEqual:@"string"]) {
        [items addObject:text];
    }
}

int main()
{
    NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];

    // Load the XML into a NSData
    NSString *xmlString = @"<?xml version=\"1.0\" encoding=\"utf-8\"?><ArrayOfString xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://tempuri.org/\"><string>Desert.jpg</string><string>Koala.jpg</string><string>Tulips.jpg</string></ArrayOfString>";
    NSData *xmlData = [xmlString dataUsingEncoding:NSUTF8StringEncoding];

    AOSParser *parser = [[AOSParser alloc] init];

    NSError *error = nil;
    NSMutableArray *items = [parser parseXMLData:xmlData parseError:&error];

    if (error) {
        NSLog(@"Error: %@", [error localizedDescription]);
    }
    else {
        NSLog(@"Items: %@", items);
    }

    [parser release];
    [items release];

    [myPool drain];
    return 0;
}
@end

输出:

Items: (Desert.jpg, Koala.jpg, Tulips.jpg)

我的第一个 Objective C 程序。

于 2012-07-17T11:21:39.507 回答