Operating system requirements: iOS 5.0 or later.
Mobile device: iPad.
This example shows some methods of working with attributes and properties of dashboard XML element. After starting the example the following operations are executed:
XML element is retrieved from dashboard object.
It is checked if the attribute exists, and attribute value by specified name.
A child XML element is retrieved from dashboard XML element.
It is checked if child XML element property value exists and is retrieved by specified name.
A new XML element is created.
Binary data is set for a new XML element, and also a child XML element is created and added with attributes of binary, integer, real and string type.
String data is set for the child XML element, and also name of the parent XML element is set.
Executing the example requires a file named SplittedListReportCell-procedural.png. Then it is required to place the following code in the boy of the executeExample method of the ViewController class (see the Displaying of Dashboard section):
// Get dashboard controller
MADashboardReportViewController* dashboard = (MADashboardReportViewController *)m_controller;
// Get dashboard
APKap *report = [dashboard report];
// Take XML object from dashboard
APXMLParserObject *xmlObject = [report data];
// Check if XML object contains attribute with specified name
BOOL hasAttribute = [xmlObject hasAttributeForKey:@"version"];
// Get value for found attribute
NSString *version = hasAttribute? [xmlObject stringAttributeForKey:@"version"] : @"not defined";
// Display attribute value in the development environment console
NSLog(@"%@ element version: %@",[xmlObject elementName] , version);
// Get child XML object in the array of nested objects
APXMLParserObject *xmlSecondObj = [[xmlObject objects] objectAtIndex:0];
// Check if child object contains property with specified name
BOOL hasProp = [xmlSecondObj hasPropForKey:@"name"];
if(hasProp == YES)
{
// Get and display value of the property with specified name in the development environment console
NSString *nameProp =[xmlSecondObj stringPropForKey:@"name"];
NSLog(@"Value of the "name" property of the child element: %@", nameProp);
}
// Create a new root XML element
APXMLParserObject *createdXmlObject = [APXMLParserObject parserObjectWithName:@"rootObject" attributes:nil];
// Get path to file
NSString *path = [[NSBundle mainBundle] pathForResource:@"SplittedListReportCell-procedural" ofType:@"png"];
// Get binary data of file
NSData *nsData = [NSData dataWithContentsOfFile: path];
// Set binary data for a new root XML element
[createdXmlObject setCdata:nsData];
// Create a dictionary containing attributes for a new child element
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
// Create a place real attribute to dictionary
NSNumber *propD = [NSNumber numberWithDouble:5.5];
[attributes setObject: propD forKey:@"doubleAttribute"];
// Create and place integer attribute to dictionary
NSNumber *propI = [NSNumber numberWithInt: 42];
[attributes setObject: propI forKey:@"intAttribute"];
// Create and place logical type attribute to dictionary
[attributes setObject: @"TRUE" forKey:@"boolAttribute"];
[attributes setObject: @"String" forKey:@"stringAttribute"];
// Create a new child XML element
APXMLParserObject *child = [APXMLParserObject parserObjectWithName:@"Name1" attributes:attributes];
// Add child XML element to the new root element
[createdXmlObject addObject:child];
// Set string data for the new child element
[child setCharacters: @"String data"];
// Get new child element attributes
NSMutableDictionary *childAttributes = [child attributes];
// Get logical type value for specified attribute in the new child element
BOOL boolAttr = [child boolAttributeForKey:@"boolAttribute"];
// Get integer value for specified attribute in the new child element
int intAttr = [child intAttributeForKey:@"intAttribute"];
// Get real value of specified attribute in the new child element
double doubleAttr = [child doubleAttributeForKey:@"doubleAttribute"];
// Get string value of specified attribute in the new child element
NSString *stringAttr = [child stringAttributeForKey:@"stringAttribute"];
// Enter obtained information to the development environment console
if([createdXmlObject cdata] != nil) NSLog(@"New root element contains binary data");
else NSLog(@"New root element does not contain binary data");
NSString *parentName = [[child parent] elementName];
NSLog(@"Parent element name for new child element: %@", parentName);
NSLog(@"String data of new child element: %@", [child characters]);
NSLog(@"Logical type value for the boolAttribute attribute: %@", boolAttr? @"YES" : @"NO");
NSLog(@"Integer type value for the intAttribute attribute: %d", intAttr);
NSLog(@"Real type value for the doubleAttribute attribute: %f", doubleAttr);
NSLog(@"String type value for the stringAttribute attribute: %@", stringAttr);
After executing the example the development environment console displays information about obtained and created XML elements, and also the result of binary data addition to the new root XML element:
kap element version: 7.1
Value of the "name" property of the child element: Dashboard
New root element contains binary data
Name of parent element for the new child element: rootObject
String data for the new child element: String data
Logical type value for the boolAttribute attribute: YES
Integer type value for the intAttribute attribute: 42
Real type value for the doubleAttribute attribute: 5.500000
String type value for the stringAttribute attribute: String
See also: