Where can I find an Objective C code which parses any XML file without knowing before any tag or attribute? - iphone

I would like to find a sample of code written in Objective C for iPhone which can parse any XML file, even if we don't know tags or attributes. Does anyone has something like that?

Probably you can convert the XML into a NSDictionary which then can be used at your ease.
I have not used this code, but maybe you can try this to convert your xml into dictionary
http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/

Related

Identifying and formatting XML String to readable format in XMLParser

I am working in Swift and I have a set of Data that I can encode as a String like this:
<CONTAINER><Creator type="NSNull"/><Category type="NSNull"/><UMID type="NSArray"><CHILD>d1980b265cbd415c90f5d5f04efcb5df</CHILD><CHILD>7e0252c137c249fc92bd0f844effe27f</CHILD></UMID><Channels type="NSNumber">1</Channels></CONTAINER>
I am looking for a way to format this string as XML with indents so I can use XMLParser to properly read through it, which it currently does not. I imagine NSNull is when the object is empty, I just haven't seen this format so I don't know what to search for. Could it be closer to a Dictionary object? If so I'd be happy to format it as that as well.
I've also tried to create a XMLDocument from the data, but it doesn't fix the format.
EDIT:
I wanted to add a bit more information to help clarify what I am trying to do. This string above is derived from an encrypted piece of metadata from a file. In my code I identify the chunk of data that is encrypted, then decrypt it, and then convert that data to a string. It's worth noting that the string ends up having null characters in between each valid character, but I strip those out and end up with this string.
Copying this string into an XML Validator confirms it is valid XML. What is confusing to me is it's format, in which it has Object types such as NSNull and NSNumber. My post was originally hoping to identify this type of format. It seems like more than just XML.
In response to some of the comments, I have used XML Parser delegate with other XML strings and have a basic understanding of how it works. I should have originally mentioned that and instead said that XML Parser does not recognize any of these elements or strings within them.
UPDATE:
The issue ended up being the null characters in between each valid character. Stripping those out and then running it through XML Parser worked great. Thanks all.

Converting Objects to plist file then to data

I have an array of objects that consist of several strings, I need to push these up to a web service as XML data. What are the steps involved here? As far as I know I need to convert the objects to a plist file, then convert this file to NSData (?) I can't find anything online that really lays it out..
Any help would be greatly appreciated.
Another approach is JSON. SBJSON is very common and simple to use.
It's a two-liner to get a json string from your array. Then you'll create an NSURLRequest that represents the post and an NSURLConnection that performs the request. Lot's of resources for that on SO and elsewhere.
You can try this or this. I doubt plist files are what you want. All you need is to serialize your object into the xml format your web service requires.

XML Parser on MonoTouch?

I want to parse XML formatted string. How to use XML parser on MonoTouch?
Exactly the same way you would in standard C#.
Your options include:
XmlSerializer - good if you want to translate a full document to a set of C# objects
XmlDocument - good if the document is custom beyond XmlSerializer can handle
XPath - good for pulling out small pieces of data, if you don't care about the whole doc.
Linq2XML - another option using Linq.
Depending on what exactly you need.
You can use LINQ to XML in MonoTouch.
So,
var element = XElement.Parse("<cat>dog</cat>");
Console.WriteLine(element.Value);
prints "dog".
You canĀ“t use System.Xml.Linq in full, there will be a JIT part that will blow up when testing in the device, see Xamarin Monotouch limitations:
link

How to create XML editor for iPhone?

I want to create a XML editor (form view type) for iphone. Can anyone suggest me how to proceed in this regard. I know how to parse an XML document but I am having trouble in editing the contents of the file dynamically. This editor should be such that, a new node can be added or a node can be deleted, it can also edit the values.
Please help...
In short you need to parse the document into a readable structure, finding a way to modify that structure, and write it back to XML.
Classes you are likely to need to write include MyXMLDocument, MyXMLNode, MyXMLElement and MyXMLTextNode, and MyXMLNode likely needs NSArray *children;, MyXMLNode *parent; and MyXMLDocument *document; as ivars.
You also probably need something akin to a -stringValue method; and an understanding of how the XML DOM usually works.

How do I read and write XML in Cocoa Touch?

I want to create a file using Objective-C, which stores the data comes from XML. I also have to do basic functions of read and write into that file. How can I do this?
You can parse a custom schema using the NSXMLParser class. This is especially useful since the NSXMLDocument class unfortunately does not exist on the iPhone. Thankfully, NSXMLParser is pretty easy to use. I've written an RSS feed parser using NSXMLParser in under half an hour.
If you have some flexibility over the XML structure, you could look at using the in-built commands to load and save a dictionary or array from/to a file, such as:
NSDictionary *myDict = [[NSDictionary alloc] initWithContentsOfFile:myFileName];
This will load in an XML file into a dictionary. It expects the file to be in the format
<key>Object 1</key><string>ContentsOfObject1</string>
<key>Object 2</key><string>ContentsOfObject2</string> etc
(You can also init an array in the same way. The file is simpler, basically just leaving out the "key" part).
Then, to save it you just use the following command:
[self.myArrayorMyDictionary writeToFile:fileFullName atomically:YES];
Hope that helps!