ADC Home > Reference Library > Technical Q&As > Core Foundation > Data Management >

CFXML to CFPropertyListRef (and back!)


Q: Is there an easy way to save and restore a CFPropertyListRef to and from an XML file?

A: For all the details see: <http://developer.apple.com/documentation/CoreFoundation/Conceptual/CFPropertyLists/Tasks/Saving.html>

For "just the code":

 

// ---------------------------------
// save a property list into an XML file:
SInt32 SavePropertiesToXMLFile(
                        const CFPropertyListRef pCFPRef, 
                        const CFURLRef pCFURLRef)
{
    CFDataRef xmlCFDataRef;
    SInt32 errorCode = coreFoundationUnknownErr;

    // Convert the property list into XML data.
    xmlCFDataRef = CFPropertyListCreateXMLData(
                        kCFAllocatorDefault, pCFPRef );
    if (NULL != xmlCFDataRef)
    {
        // Write the XML data to the file.
        (void) CFURLWriteDataAndPropertiesToResource( 
                        pCFURLRef, xmlCFDataRef, NULL, &errorCode);
    
        // Release the XML data
        CFRelease(xmlCFDataRef);
    }

    return errorCode;    
}

// ---------------------------------
// load a property list from an XML file
CFPropertyListRef CreatePropertiesFromXMLFile(const CFURLRef pCFURLRef)
{
    CFDataRef xmlCFDataRef;
    CFPropertyListRef myCFPropertyListRef = NULL;
    Boolean status;

    // Read the XML file.
    status = CFURLCreateDataAndPropertiesFromResource( 
                        kCFAllocatorDefault, pCFURLRef, 
                        &xmlCFDataRef, NULL, NULL, NULL);
    if (status)
    {
        // Reconstitute the dictionary using the XML data.
        myCFPropertyListRef = CFPropertyListCreateFromXMLData(
                        kCFAllocatorDefault, xmlCFDataRef, 
                        kCFPropertyListImmutable, NULL);
        // Release the XML data
        CFRelease(xmlCFDataRef);
    }

    return myCFPropertyListRef;
}

Listing 1. CFXML <-> CFPropertyList


[Aug 29, 2003]


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.