0

ねえ、誰でも助けてください。私はxcode(目的のc)で新しく、asp.net Webサービスを使用しています。そのサービスから3つのファイル名をxcodeプロジェクトに返すだけです。今、私がやりたいことは解析することですXML の結果の Web サービス データを 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プロジェクトに次の4つのクラスファイルがあります

imagesavetestviewcontroller.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

viewcontroller.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 ファイルをアップロードまたはリンクすると、Web サービスと通信するための実行可能なコードを含む zip を取得できます。

「sudzc」とタグ付けされた質問もたくさんあります。


NSXMLParserXML を読み取るために使用することもできます。

(デモ)

#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 に答える