Operating system requirements: iOS 5.0 or later.
Mobile device: iPad.
This example shows some methods of search for embedded objects in dashboard XML element. After starting the example the following operations are executed:
Dashboard XML element is retrieved by means of the APXMLParser.parseXML: method.
A child XML element is retrieved from array of embedded XML element objects.
An embedded XML element is retrieved from the child XML element by specified tag.
Integer, real and logical values of some properties are retrieved from the obtained element by specified keys.
The embedded XML element that represents the specified property is retrieved from the obtained element by specified key.
The obtained XML object name is retrieved.
An array of embedded XML elements with specified key is retrieved from dashboard XML element.
An XML element is searched by specified key at the top nesting level of parent and root XML elements.
An XML element is searched by specified key at all nesting levels with the start of search in the root XML element.
An XML element is searched by specified name at all nesting levels with the start of search in the root XML element.
An XML element is searched by specified name at all nesting levels with the start of search in the previously found element.
An array of all nested XML elements of the child element is retrieved by specified name.
An array of XML elements by specified name at the first nesting level of the child XML element.
An XML element is searched by specified name at the top nesting level of the parent and root XML element.
Executing the example requires to place the following code in the body of the executeExample method of the ViewController class (see the Displaying of Dashboard section):
// Get dashboard controller
MADashboardReportViewController* dashboard = (MADashboardReportViewController *)m_controller;
// Get path to XML file describing dashboard
NSArray *resourcesPaths = [[dashboard delegate] dashboardResourcesPath];
NSString *xmlPath = (NSString *)[resourcesPaths objectAtIndex:0];
// Get XML file data
NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];
// Get dashboard XML object
APXMLParserObject *xmlObject = [APXMLParser parseXML:xmlData];
// Get child XML object in array of embedded objects
APXMLParserObject *xmlSecondObj = [[xmlObject objects] objectAtIndex:0];
// Get embedded XML element with the "background" tag
APXMLParserObject *objectWithTag = [xmlSecondObj objectWithTag:@"background"];
if(objectWithTag != nil)
{
// Get integer value of property with the "gradientAngle" key
BOOL hasProp = [objectWithTag hasPropForKey:@"gradientAngle"];
int prop = hasProp?[objectWithTag intPropForKey:@"gradientAngle"] : 0;
// Get real value of property with the "gradientAngle" key
double prop2 = [objectWithTag doublePropForKey:@"gradientAngle"];
// Get logical value of property with the "useBackground" key
BOOL prop3 = [objectWithTag boolPropForKey:@"useBackground"];
// Get XML element that is a property with the "backgroundColor" key
APXMLParserObject *backgroundColorObject = [objectWithTag objectPropForKey:@"backgroundColor"];
// Get XML element name with the "backgroundColor" key
NSString *elementName = [backgroundColorObject elementName];
// Display obtained information in the development environment console
NSLog(@"Integer value of property with the "gradientAngle" key: %d", prop);
NSLog(@"Real value of property with the "gradientAngle" key: %f", prop2);
NSString *boolString = prop3? he @"YES": @"NO";
NSLog(@"Logical value of property with the "useBackground" key: %@",boolString);
NSLog(@"XML element name with the "backgroundColor" key: %@", elementName);
}
else
{
NSLog(@"Element with the "background" tag is not found");
}
// Get array of embedded XML elements, for which key is set
NSDictionary *objectsWithIDs = [xmlObject objectsWithIDs];
// Get array of XML element keys, for which key is set
NSArray *objectsKeys = [objectsWithIDs allKeys];
// Get last key in array
NSString *key = [objectsKeys objectAtIndex:[objectsKeys count] - 1];
// Get XMl element by key
APXMLParserObject *objectWithID = [objectsWithIDs valueForKey: key];
// Save key value in a new string
NSString *idOfObject = [objectWithID ID];
// Get parent element for obtained XML element
APXMLParserObject *parentObject = [objectWithID parent];
// Search element by key at top nesting level of parent element
APXMLParserObject *objectForID = [parentObject objectForID: idOfObject];
// Search element by key at top nesting level of root element
APXMLParserObject *objectForID2 = [xmlObject objectForID: idOfObject];
// Search element by key at all nesting levels starting from the root element
APXMLParserObject *objectForID3 = [xmlObject findObjectForID: idOfObject];
// Display element search results in the development environment console
NSString *rootName =[xmlObject elementName];
NSString *parentName =[parentObject elementName];
if(objectForID != nil)
{
NSString *objectForIDName = [objectForID elementName];
NSLog(@"The %@ element is found at top nesting level of the %@ element", objectForIDName, parentName);
}
else NSLog(@"Element with the %@ key is not found", idOfObject);
if(objectForID2 != nil)
{
NSString *objectForID2Name = [objectForID2 elementName];
NSLog(@"The %@ element is found at top nesting level of the %@ element", objectForID2Name, rootName);
}
else NSLog(@"Element with the %@ key is not found", idOfObject);
if(objectForID3 != nil)
{
NSString *objectForID3Name = [objectForID3 elementName];
NSLog(@"The %@ element is found at nesting levels of the %@ element", objectForID3Name, rootName);
}
else NSLog(@"Element with the %@ key is not found", idOfObject);
// Set the name, by which elements are searched
NSString *nameOfObject = @"prop";
// Element is searched at all nesting levels by specified name
APXMLParserObject *objectOfName = [xmlObject findObjectOfElementName:nameOfObject];
if(objectOfName != nil)
{
// Search element at all nesting levels by specified name starting from previously found element
APXMLParserObject *objectOfName2 = [xmlObject findObjectOfElementName:nameOfObject startingAt:objectOfName];
if(objectOfName2 != nil)
{
// Compare obtained elements and display comparison result in the development environment console
if([objectOfName2 isEqual:objectOfName] == YES) NSLog(@"Elements obtained by name by two methods are equal");
else NSLog(@"Elements obtained by name by two methods are not equal");
}
else NSLog(@"The %@ element is not found at nesting levels of the first found element", nameOfObject);
}
else NSLog(@"The %@ element is not found", nameOfObject);
// Get array of all embedded XML elements of child element by specified name
NSArray *findedObjects = [xmlSecondObj findObjectsOfElementName: nameOfObject];
// Get array of XML elements by specified name at the first nesting level of child element
NSArray *findedObjects2 = [xmlSecondObj objectsForElementName: nameOfObject];
// Display information about obtained arrays in the development environment console
NSLog(@"Number of elements named %@ obtained at all nesting levels of the child element: %d", nameOfObject, [findedObjects count]);
NSLog(@"Number of elements named %@ obtained at the top nesting level of the child element: %d", nameOfObject, [findedObjects2 count]);
// Set new name, by which elements are searched
nameOfObject = @"area";
// Search element by name at the top nesting level starting from the root element
APXMLParserObject *objectOfName3 = [xmlObject objectForElementName: nameOfObject];
// Search element by name at all nesting levels starting from the root element
APXMLParserObject *objectOfName4 = [xmlObject findObjectOfElementName: nameOfObject];
//Display element search results in the development environment console
if(objectOfName3 != nil)
{
NSLog(@"The %@ element is found at the top nesting level of the root element", nameOfObject);
}
else NSLog(@"The %@ element is not found at the top nesting level of the root element", nameOfObject);
if(objectOfName4 != nil)
{
NSLog(@"The %@ element is found in the root element hierarchy", nameOfObject);
// Get parent element of the found element
APXMLParserObject *parentObject2 = [objectOfName4 parent];
// Search element by name at the top nesting level of the parent element
APXMLParserObject *objectOfName5 = [parentObject2 objectForElementName: nameOfObject];
if(objectOfName5 != nil)
{
NSString *parentObject2Name = [parentObject2 elementName];
NSLog(@"The %@ element is found at the top nesting level of the %@ element", nameOfObject, parentObject2Name);
}
else NSLog(@"Element with the %@ key is not found", idOfObject);
}
else NSLog(@"The %@ element is not found in the root element hierarchy", nameOfObject);
After executing the example the development environment console displays information about obtained values of XML elements, name of the XML element obtained by means of the specified key, element search results by various methods, information about arrays of elements found by various methods, and also result of comparison of the elements found by various methods by the same name:
Integer value of the property with the "gradientAngle" key: 270
Real value of the property with the "gradientAngle" key: 270.000000
Logical value of the property with the "useBackground: key: NO
XML element name with the "backgroundColor" key: prop
The "block" element is found at the top nesting level of the "area" element
Element with the 7HBEWOKE7UKGM6VB key is not found
The "block" element is found at nesting levels of the "kap" element
Elements obtained by name by two methods are not equal
Number of elements named "prop" obtained at all nesting levels of the child element: 300
Number of elements named "prop" obtained at the top nesting level of the child element: 4
Element named "area" is not found at the top nesting level of the root element
The "area" element is found in the root element hierarchy
The "area" element is found at the top nesting level of the "prop" element
See also: