Creating XML with numeric strings
I'm creating an XML document from an array of NSDictionarys as follows:
for (NSDictionary *info in sorted)
{
NSArray *item = [NSArray arrayWithObjects:
[NSXMLElement elementWithName: @"name" stringValue: [info objectForKey: @"Name"]],
[NSXMLElement elementWithName: @"year" stringValue: [info objectForKey: @"Year"]],
nil];
node = [NSXMLElement elementWithName: @"item" children: item attributes:nil];
[root addChild:node];
}
NSData *xmlData = [xmlDoc XMLDataWithOptions:NSXMLNodePrettyPrint];
I then write out the NSData object. That all works fine.
The problem is that the values for the "year" entity are bein开发者_如何学JAVAg converted to scientific notation. That is, if the original string in the info dictionary is "1998", what ends up in the XML file is:
<year>1.998E3</year>
I tried putting quotes around the string when adding the element, but then I get quotes in the output XML:
<year>"1998"</year>
Any ideas?
Apple's documentation for setObjectValue in NSXMLNode is suggestive:
Note: Prior to Mac OS X v 10.6 setObjectValue: would improperly and inconsistently format objects that were NSNumber instances. Applications linked on Mac OS X 10.6 or later will use correct scientific notation for all NSNumbers passed to setObjectValue:. If you require a particular format for any value in your XML document, you should format the data yourself as a string and then use setStringValue: to set the value. This guarantees that the text generated is in a format you control directly.
精彩评论