By the way, let's quickly go through some very simple questions before starting with the code. Try to understand the following and make sure you know the background of what I'm going to perform here.
XML... what's it about?
XML stands for Extensible Markup Language and it is a markup language (everything is marked as a tag) that defines a set of rules for encoding documents in a format that both human-readable and machine-readable.
Why XML is so popular?
I don't think that anyone in the field of Information Technology will ever tell that they have never heard the word XML since it is so popular because the design goals of XML highlight simplicity, generality and usability over the Internet. XML files are simply text files and no mater of the platform or technology we are using XML can be understood by all of them.
Well Formed Vs Valid XML Documents
A "Well Formed" XML Document has correct XML syntax. For an example,
- XML documents must have a root element
- XML elements have a closing tag
- XML tags are case sensitive
- XML elements must be properly nested
- XML attribute values must be quoted
A "Valid" XML document is a "Well Formed" XML document which is also confirms to the rules of Document Type Definition (DTD)
How to Validate your XML file?
Let's say that you have an XML file with you and you want to validate it to check for it's syntax. you have a number of tools to do this but let's make it simple. Follow the below links to find out how this task can be easily done through online tools within few seconds. All what you have to do is to simply copy paste the content of your XML file or browse your XML file in the given URL and use the validator to check your file.
OK, now let's move to the most interesting part of our post today. As I told you earlier, we need to have a valid and well formed XML file and it is going to be the below shown file.
File content is as below,
<service name="Service1" url="/utilities/service1.xml">
<service name="Service2" url="/utilities/service2.xml">
OK, Now we have simply added the required XML file to our XCode project. Now it's time to get the content of the XML file and start parsing it using our app.
By the way, Do you know how to read an embedded file from an XCode project in Objective-C?
It's simple. Just a matter of adding few lines of code to get the file content to an NSString. Refer the code below.
*Function reads the embedded SampleXMLFile.xml file and saves to an instance variable call configXmlData of type NSData
*@return : no data returned
//reading the resources from the mainBundle of where it search for a file named SampleXMLFile with file extension of xml
NSString *configXmlFilePath=[[NSBundle mainBundle] pathForResource:@"SampleXMLFile" ofType:@"xml"];
//if the file exist, then get the contents of the file to the configXmlData variable
//if incase of an error occurs (file doesn't exist or user has no privilege to access it etc ), then print this error message in NSLog
@catch (NSException *exception)
NSLog(@"Exception Occurred in reading setConfigurationFile: %@ ",exception);
Now Let's turn on the XPath related work now. Keep in mind that I am going to use some files for this purpose that are available in GitHub in the Hppl Project. Use the below shown link to access the Hppl project.
Access Hppl Project From Here
download the project above and add these files to your project.
What Am I going to do now...
Ok, let's come to the main target of this post. Here's what I am going to do. I hope you have noticed the structure of the above shown XML file. All what I m going to do is to extract the data of that file as I wanted. Let's say I don't need the entire file content always but a part of it is sufficient for me to perform a task as shown below.
When I pass a string called 'Service1' then the app should display the output as below.
when I pass s string 'Service2' then the app should display the output as below.
My plan is to write a simple XML parser function to read tags, attributes, elements and values of the XML file accordingly with the help of the methods included in Hppl Project that we have just added to the project. It's really easy and all what you have to pay a little more attention is for the XPath query that you are suppose to write to extract the matching XML elements.
For simplicity, I'll explain you the methods one by one and then you can download the sample app and monitor how things are handled as a whole.
Note that MetaConfigurationHandler is the name of the class that I have and you need to add two more reference for it as below.
*Function creates an NSDictionary containing all the data required for a specific http connection as key value pairs
*@return : an NSDictionary containing the required key value pairs
-(NSDictionary *)generateConfigurationDictionary:(NSString *)request
//calls the above mentioned function to read the embedded xml
//file contents to the instance variable configXmlData
//holds each value for each key of the dictionary
//holds each key of the dictionary
//holds the name of the tag
//holds the xPath query (what matching string we are searching for)
//holds all the elements of the Services tag
NSArray *configurationServiceElements=[NSMutableArray array];
//this will be returned as an output containing the aforementioned
//NSMutableDictionary with keys and values respectively
NSMutableDictionary *configurationDictionary =[NSMutableDictionary dictionary];
//xpath query string to search for the children of service tag
//where the service's name= request (here, request is a string
//as Service1 or Service2 that is passed to this function as
//passing the xPath query, retrieving all the tags that are
//matching with the above xPath query
//configurationServiceElements array will hold elements like url,
//type,ID,NAMEOFDEVICE,SERIALOFDEVICE,IPADDRESS and STATUS
//valueForKey is going to hold /utilities/service1.xml
valueForKey=[[configurationServiceElements objectAtIndex:0] objectForKey:@"url"];
//add value=/utilities/service1.xml where key=url
//(when request=Service1) which
//means configurationDictionary will hold one entry with a key-value pair
[configurationDictionary setObject:valueForKey forKey:@"url"];
//clear the xPathQuery
//xpath query string to search for the value under the tag named as type and
//which is a sub element od Services/service where service name is equal to
//pass the query to get the value
//how to get the value of the tag which is type
valueForKey=[[[configurationServiceElements objectAtIndex:0] firstChild] content];
//add this key value pair to the dictionary, configurationDictionary
[configurationDictionary setObject:valueForKey forKey:@"type"];
/*Now, I'm trying to get all the tags under /services/service that are named as
parameter-mapping, then get(element) each of its attribute values and element values to form the key-value pairs for the dictionary as
//first the xPath query search for parameter-mapping elements
xPathQuery=[NSString stringWithFormat:@"/services/service[@name='%@']/parameter-mapping", request];
//get all those elements that matches the xpath query above to an array
configurationServiceElements=[self getConfigurationElementArray: xPathQuery];
//now for each element in the configurationServiceElements array...
for (TFHppleElement *element in configurationServiceElements)
//get the name of the tag and assign to tagName variable
//check if that tagName is equal to parameter-mapping
//if true, assign it to key
//get the attribute of parameter-mapping which is enumvalue and
//make it a key
//get the value of the attribute named key (which means enumvalue)and
//assign it to valueForKey
//now add those key-value pair to the configurationDictionary as
[configurationDictionary setObject:valueForKey forKey:key];
//if incase an exception occurs then handle it by printing the exception
@catch (NSException *exception)
NSLog(@"Exception Occurred in generateConfigurationDictionary:%@ ",exception);
//finally, return the configurationDictionary NDmutableDictionary
*Function executes an xpath query and returns the matching nodes as an NSArray to the callee
*@xPathQuery : xpath query as a string should be passed
*@return : an NSArray containing the matching elements will be returned
-(NSArray *)getConfigurationElementArray:(NSString *)xPathQuery
TFHpple *servicesParser = [TFHpple hppleWithXMLData:configXmlData];
NSString *servicesXpathQueryString = xPathQuery;
servicesNodes = [servicesParser searchWithXPathQuery:servicesXpathQueryString];
Now, the most required or I would rather say most difficult part of our XML parsing is done. All what you have to do is to make relevant function calls passing proper parameters to get the output as you want.
for an example, I can call the parser functions as shown below. Assume that I am going to call these functions from another class
MetaConfigurationHandler *meta=[[MetaConfigurationHandler alloc]init];
NSDictionary *configDic=[NSDictionary dictionary];
Now you can simply print the configDic contents in NSLog or as you preferred.
Hope this post was interesting to you guys :)
Further reading :