如何在IOS中将xml Document转换为NSString

时间:2013-04-04 06:42:28

标签: iphone ios xml xml-serialization

我在ios中创建xml,这是正确的(在日志上打印),但是当我将xmlDocument转换为NSString时,当我在日志中打印时,字符串包含null,需要sting作为webservice的参数,如何将xmlDocument分配给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);

4 个答案:

答案 0 :(得分:3)

试试这个,

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

答案 1 :(得分:1)

试试这个。

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

  NSLog(@"%@",stringXml);

答案 2 :(得分:0)

这是正确的方法,否则,您将丢失xml元素,从而丢失xml版本和文档编码。

GDataXMLDocument * xmlDocument = [[GDataXMLDocument alloc] initWithRootElement:rootElement];

[xmlDocument setVersion:@"1.0"];
[xmlDocument setCharacterEncoding:@"UTF-8"];

NSString *xmlDocumentRepresentation = [[NSString alloc] initWithData:xmlDocument.XMLData encoding:NSUTF8StringEncoding];

NSLog(@"xml %@", xmlDocumentRepresentation);

答案 3 :(得分:-1)

你应该这样试试,更容易:

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

我用这种方式很多。祝你好运!