I am trying to parse the xml file in xcode and store data in different arrays for different keys.My xml file is like as follows:
<Colleges>
<College>
<id>1</id>
<Name>abc</Name>
<Branches>
<Branch>a</Branch>
<Branch>b</Branch>
</Branches>
</College>
..... And many colleges....
</Colleges>
Can any one please give me the sample code to parse this file and put in separate arrays. I have knowledge of delegate methods but failed to implement them. I think I am lacking some were in between.
Try this: Simple XML to NSDictionary Converter. The two files will help you to convert your XML data to NSDictionary from which you can easily get your array.
Related
Scenario 1 - I have some XML files stored in FTP.Those files are being fetched by FTP connector in Azure logic app. Then I am reading those files by parsing it into JSON & storing those objects in String variables for my operation. Then after my processing I want to convert that json back to XML for the output.
Scenario 2 - I am merging multiple XMl files(all are of same structure) into an single one. after merging I can get the output in JSON format but then I want to convert the same into XML format.
So please suggest how can I convert JSON to XML through Logic App & Azure function only.
Try the 'xml' function.
screenshot of xml function example in Logic App
Make sure that your JSON input is structured suitably for conversion to XML, for example you should only have a single element at the top level, which will form your XML root element.
I have a JSON file like this. I have to make bold part of string which is shown in JSON. How can I make parse this JSON?
It looks to me like you would first want to use NSJSONSerialization (Or just JSONSerialization in Swift 3) to convert your JSON to an object graph. Once you've done that, you should be able to navigate to the interestLabel keys in your data and fetch those strings.
You'll then need to parse those tagged strings somehow. If the only thing you need to do is to find <b> and </b> bold tags, and no other tags will ever appear in your data then you could probably write your own code. If the strings might have other tags and/or more complex HTML structure then you might want to use an XML/HTML parser. I suggest taking a look at this tutorial: https://www.raywenderlich.com/14172/how-to-parse-html-on-ios
I'm looking for the best way (or easiest) to import data into my iOS app using Swift. I've got a file containing recipes and I have to read in the recipe names and instructions.
Example:
Fudge brownies
Mix ingredients in processors until consistent.
Prepare baking sheet with coconut oil and set over at 425.
....
So I have to import several dozen recipes, my questions are
Would it be best to read in a text file?
If so how is this done in Swift?
Also how do I avoid issues with reading the title and recipe into separate variables?
You can read in a text file quite easily doing something like this:
let path = NSBundle.mainBundle().pathForResource("fileName", ofType: "txt")
var dataString = String(contentsOfFile: path!, encoding: NSUTF8StringEncoding, error: nil)
Note you'll have to import Foundation.
You could then create a method to parse the dataString, something like
func parseDataString(string: String)
which you could send the dataString to.
You could put markers (e.g. special characters like (*) ) in the text file that would allow this method to figure out where the titles end and the directions start. There are a number of ways it could be done.
You could then persist your data using CoreData.
I would strongly suggest using JSON data in the files. JSON is a very simple markup format that gives structure to text files, and lets you basically say things like title=BBQ ribs. The reason you use JSON is that Cocoa has really good JSON handling built right in. Check out this thread, it probably does exactly what you want...
How do I parse JSON with Objective-C?
I was thinking to have a category on NSXMLParser with one class method (BOOL)isUnEncryptedXML: and in the implementation try to initialise a local instance of NSXMLParser using its initWithContentsOfURL:
The assumption is, if it won't initialise, it can't read the XML, so it is not unencrypted.
Would you find such an implementation naive? Is it reliable in the context of my question?
you can do 2 things.
an xml file has to begin with <?xml
an xml file that is encrypted likely won't parse. So try parsing it and if it isn't well formed the parser will fail
If 1&2 fail then the file is invalid :encrypted / broken
If only 1 fails, then you could ASSUME it is encrypted
you can't really tell encrypted/broken by looking at the xml
Consider the XML below:
<root>
<bar>
<p>Hello <italic>World</italic></p>
</bar>
<banjo><p>Hello <italic>World</italic></p></banjo>
</root>
I am currently using https://github.com/nicklockwood/XMLDictionary to parse XML data. I would like to ignore <p> and <italic> tags. How can I achieve that?
NSDictionary should somehow look like this structure:
-root
--bar
---<p>Hello <italic>World</italic></p>
--banjo
---<p>Hello <italic>World</italic></p>
So a quick glance at that XMLDictionary class seems to show that it just wraps the standard NSXMLParser and creates a convenient dictionary object with the all of the XML elements.
If you are committed to that github code, then you could potentially go through your resultant dictionary after parsing the XML and restructure the <p> and <italic> elements back into the parent element. But that's pretty inefficient and definitely not recommended unless you have very short XML and the benefit of the XMLDictionary class warrants the extra work.
You could also dig into the XMLDictionary code, specifically the parser:didStartElement and parser:didEndElement methods, and make some changes to ignore the <p> and <italic> tags. This should effectively flatten the structure to something close to your example result.
But, if you're considering poking around in that code, you might as well just use the NSXMLParser directly, ignoring those tags in the same way. This will give you tighter control over parsing and could reduce some unnecessary object creation, especially if your XML is of significant size. Just read up on NSXMLParserDelegate and even walk through the XMLDictionary code to see how it does the things you like about it.