2

我在 ios 中创建 xml,这是正确的(在日志上打印),但是当我将xmlDocumentin转换为时NSString,字符串包含 null 当我在日志中打印时,需要 sting 作为 webservice 的参数,如何分配xmlDocumentNSString...谢谢..

我的代码是...

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

4 回答 4

3

试试这个,

NSString * YOURSTRING = [[NSString alloc] initWithData:[xml rootElement] encoding:NSUTF8StringEncoding];
NSLog(@"%@",YOURSTRING);
于 2013-04-04T07:23:26.873 回答
1

试试这个。

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

  NSLog(@"%@",stringXml);
于 2013-04-04T06:48:47.910 回答
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);
于 2018-10-30T21:43:13.463 回答
-1

你应该尝试这种方式,更容易:

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

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

于 2015-01-12T04:13:39.733 回答