include "NSLog.incl" include "Tlbx XML.incl" local fn FormattedXMLTest // Input data CFArrayRef names = @[@"April", @"Tam O'Shanter", @"Emily"] CFArrayRef remarks = @[@"Bubbly: I'm > Tam and <= Emily", @"Burns: \"When chapman billies leave the street ...\"", @"Short & shrift"] // Root element XMLElementRef root = fn XMLElementWithName( @"CharacterRemarks" ) // Create each element for NSInteger i = 0 to fn ArrayCount(names) - 1 XMLElementRef character = fn XMLElementWithName( @"Character" ) // Add name attribute XMLNodeRef nameAttr = fn XMLNodeAttributeWithName( @"name", names[i] ) XMLElementAddAttribute( character, nameAttr ) // Add remark as text content XMLNodeRef textNode = fn XMLNodeTextWithStringValue( remarks[i] ) XMLElementAddChild( character, textNode ) XMLElementAddChild( root, character ) next // Build the document XMLDocumentRef doc = fn XMLDocumentWithRootElement(root) XMLDocumentSetCharacterEncoding( doc, @"UTF-8" ) XMLDocumentSetVersion( doc, @"1.0" ) // Output file path CFStringRef path = @"/tmp/characters.xml" // Write the XML to file (pretty printed) CFDataRef xmlData = fn XMLDocumentXMLDataWithOptions( doc, NSXMLNodePrettyPrint ) CFURLRef desktopURL = fn FileManagerURLForDirectory( NSDesktopDirectory, NSUserDomainMask ) CFURLRef url = fn URLByAppendingPathComponent( desktopURL, @"XML_Test.xml" ) BOOL success = fn DataWriteToURL( xmlData, url, NSDataWritingAtomic, NULL ) if ( success == YES ) NSLog( @"XML file successfully written to: %@", url ) fn WorkSpaceOpenURL( url ) else NSLog( @"Failed to write XML file" ) end if end fn fn FormattedXMLTest HandleEvents