2013-04-04 91 views
2

我在ios中創建xml,這是正確的(打印日誌),但是當我將轉換爲NSString時,字符串在日誌中打印時包含null,sting是require作爲Web服務的參數,如何分配xmlDocumentNSString ...感謝..如何將xml文檔轉換爲IOS中的NSString

我的代碼是...

GDataXMLElement *rootElement = [GDataXMLElement elementWithName:@"Root"]; 

GDataXMLElement *zipCode = [GDataXMLElement elementWithName:@"zipCode" stringValue:self 
          .tfZipcode.text]; 
GDataXMLElement *phoneNo = [GDataXMLElement elementWithName:@"phoneNo" stringValue:self.tfPhone.text]; 
GDataXMLElement *cellNo = [GDataXMLElement elementWithName:@"cellNo" stringValue:self.tfCell.text]; 

GDataXMLElement *city = [GDataXMLElement elementWithName:@"city" stringValue:self.tfCity.text]; 
GDataXMLElement *zone = [GDataXMLElement elementWithName:@"zone" stringValue:self.tfZone.text]; 
GDataXMLElement *firstName = [GDataXMLElement elementWithName:@"firstName" stringValue:self.tfFirstName.text]; 
GDataXMLElement *lastName = [GDataXMLElement elementWithName:@"lastName" stringValue:self.tfLastName.text]; 
GDataXMLElement *address = [GDataXMLElement elementWithName:@"address" stringValue:self.tfAddress.text]; 
GDataXMLElement *email = [GDataXMLElement elementWithName:@"email" stringValue:self.tfEmail.text]; 

GDataXMLElement *service = [GDataXMLElement elementWithName:@"service" stringValue:self.tfService.text]; 
GDataXMLElement *product = [GDataXMLElement elementWithName:@"product" stringValue:self.tfProduct.text]; 
GDataXMLElement *symptom = [GDataXMLElement elementWithName:@"symptom" stringValue:self.tfSymptoms.text]; 
GDataXMLElement *jobType = [GDataXMLElement elementWithName:@"jobType" stringValue:self.tfJobType.text]; 
GDataXMLElement *ref = [GDataXMLElement elementWithName:@"ref" stringValue:self.tfRef.text]; 
GDataXMLElement *reqDate = [GDataXMLElement elementWithName:@"reqDate" stringValue:self.tfDateTime.text]; 


[rootElement addChild:zipCode]; 
[rootElement addChild:phoneNo]; 
[rootElement addChild:cellNo]; 
[rootElement addChild:city]; 
[rootElement addChild:zone]; 
[rootElement addChild:firstName]; 
[rootElement addChild:lastName]; 
[rootElement addChild:address]; 
[rootElement addChild:email]; 
[rootElement addChild:service]; 
[rootElement addChild:product]; 
[rootElement addChild:symptom]; 
[rootElement addChild:jobType]; 
[rootElement addChild:ref]; 
[rootElement addChild:reqDate]; 

GDataXMLDocument *xml = [[GDataXMLDocument alloc] initWithRootElement:rootElement]; 
NSLog(@"%@", [xml rootElement]); // print xml on log 



NSString *stringXml = [xml rootElement]; 

NSLog(@"%@",stringXml); 
+0

我想在編碼方面有些問題。 – satheeshwaran 2013-04-04 06:47:15

+0

我打印xmlDocument日誌,這是正確的..但如何將xmlDocument轉換爲NSString?那是我的問題..謝謝 – 2013-04-04 06:50:14

+0

你想直接打印GDataXMLDocument嗎? – iphonic 2013-04-04 06:55:00

回答

2

試試這個。

NSString *stringXml = [NSString stringWithFormat:@"%@", [xml rootElement]]; 

    NSLog(@"%@",stringXml); 
+0

感謝您的答覆... – 2013-04-04 08:31:03

+0

現在工作嗎? – satheeshwaran 2013-04-04 08:45:09

+0

邑,它分配給字符串,但web服務給我錯誤,, 400 – 2013-04-04 08:56:30

2

試試這個,

NSString * YOURSTRING = [[NSString alloc] initWithData:[xml rootElement] encoding:NSUTF8StringEncoding]; 
NSLog(@"%@",YOURSTRING); 
+0

感謝您的答案... – 2013-04-04 08:47:00

0

你應該嘗試這種方式,更容易:

NSString *yourXMLString = [[GDataXMLDocument rootElement] xmlString]; 

我用這種方式很多。祝你好運!