Using the High-Level Parser Interface

Listing 1-4 shows how you would use the high level XML Services API to convert the sample XML data in Listing 1-3 into a CFXMLTree. This example assumes that sourceURL is a valid CFURL and refers to the XML document.

Listing 1-4 Using the high-level parser API
CFXMLTreeRef cfXMLTree; CFDataRef xmlData; // Load the XML data using its URL. CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault, sourceURL, &xmlData, NULL, NULL, NULL) // Parse the XML and get the CFXMLTree. cfXMLTree = CFXMLTreeCreateFromData(kCFAllocatorDefault, xmlData, sourceURL, kCFXMLParserSkipWhitespace, kCFXMLNodeCurrentVersion);

Figure 1-1 illustrates the structure of the CFXMLTree produced by the code in Listing 1-4. As you would expect, it exactly reflects the structure of the original XML document. The diagram displays the data type code and data string from each CFXMLNode.

Figure 1-1 The structure of a CFXMLTree

The example in Listing 1-5 shows how to use some of the XML Services convenience functions to examine the top level of a CFXMLTree and print out each node's data string contents.

Listing 1-5 Obtaining information from a CFXMLTree
CFXMLTreeRef xmlTreeNode; CFXMLNode xmlNode; int childCount; int index; // Get a count of the top level node's children. childCount = CFTreeGetChildCount(cfXMLTree); // Print the data string for each top-level node. for (index = 0; index < childCount; index++ ) { xmlTreeNode = CFTreeGetChildAtIndex(cfXMLTree, index) ; xmlNode = CFXMLTreeGetNode(xmlTreeNode); CFShow(CFXMLNodeGetString(xmlNode)); }

© 2000 Apple Computer, Inc. (Last Updated 14 July 2000)