Is converting an XML to NSString from NSData, then manipulate string and convert it back to NSData is safe?
I heard it is always safer to work with XML Parsing libraries. Can anyone explain why? and at which points I should be careful if I will use that method? is that a possible encoding problem?
The risk is that you'll do this:
Convert an XML to a string
Manipulate the string, and accidentally break the XML.
Convert back, and end up with an invalid XML.
If you work with an XML parsing library, as you can manipulate the elements in a DOM, you won't have the chance of breaking the XML structure and ending up with an invalid XML.
Other than that, if you are careful with the operations you do on the NSString, you'll probably be fine.
Conversion between NSData and NSString is safe as long as you do the conversion with the original encoding.
// example: to NSData and back assuming the original message uses UTF-8
NSData *data = [NSData dataWithBytes:[message UTF8String] length:[message lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
NSString *string = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
Parsing XML as string will onnly work with naive documents. If your XML structure doesn't change, if the elements you search for are unique, if the contents are only characters, if there are no CDATA sections in the middle, if there are no namespaces, you are safe. Otherwise your code will get easily confused trying to digest the XML. It's going to be more solid if both the creator and the client of the document abide by the rules set by the XML standard.
If behind all this you are worrying about complexity, it's easy to operate on XML using XPath. If you worry about speed, maybe you could switch to a faster format like JSON if you are in control of XML generation.
Related
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.
Using the NSXMLParserDelegate protocol for parsing XML is fine, however I have the need to copy verbatim a chunk of XML in an answer. What I would like to do is store everything between the beginning/end XML tags verbatim as an NSString object so I can replay this fragment in a future query.
Is this possible or the only solution is parsing the tree manually, converting to a temporal object, then back to XML string in the future query?
One thing to note is that I'm not parsing incrementally the input, rather I'm creating the NSXMLParser object with the complete xml data, then calling parse on it. So maybe there's a way to correlate the position of didStartElement/didEndElement inside the original xml data so I can extract the subrange?
Both didStartElement and didEndElement are being passed an NSXMLParser which tracks the progress of the parsing through the lineNumber and columnNumber properties. Unfortunately there's no direct way to transform those line/column info to a buffer offset, but then as well you have to interpret the NSData with a specific encoding.
A solution is to transform the NSData into a buffer of unichar elements with the NSString::getCharacters:range: method. Then the unichar buffer can be iterated scanning for newline elements until a match of line/col is found against the values stored by the NSXMLParser object. Doing this for the start/end tags gets you the unichar range of characters of XML contained inside them.
Now this range can be transformed to an NSString and that be reused in future queries. The advantage of this is that the XML inside doesn't require to be parsed since it is copied directly and is expected to be well formed.
I am attempting to send a JSON representation of an object in an email link. The recipient will open the link and my app will respond via a url scheme. It must extract the JSON from the url and re-build the object.
I am serializing my object by building an NSDictionary and using:
return [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
I'm not sure what comes next. Somehow I need to convert this NSData into a string so that I can prefix my url scheme and use it in a link.
On the receiving end, I then need to remove the prefix (which I can do) and turn the string back into an NSData.
What is the correct method for doing this? And how do I make sure that the contents of my data do not interfere with the JSON string encoding (e.g. if my object contains text including special characters)?
You need to do an additional encoding step, since there are characters in encoded JSON that also have significance when they are part of a URL. What we actually want to do is URL-encode the data so none of the characters in the resulting string conflict with what applications expect a URL to look like.
The first step is transforming our data into an NSString (this is basically just a memcpy since NSStrings are encoded in UTF-8 by default):
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
Now, there's a function you might be tempted to use called -stringByAddingPercentEscapesUsingEncoding, but it doesn't do a thorough enough job of escaping all the relevant characters, so we need to build our own.
I could repeat the code here, but since it's been done many times already, just view this blog that shows how you can add a category to NSString to do proper encoding, after which you can append it and send it on its way. Writing the analogous decoding function with CFURLCreateStringByReplacingPercentEscapesUsingEncoding is an exercise for the reader of which many examples can be found floating around.
Make sure your payloads are quite small (on the order of a couple of kB), by the way, since there is probably an upper bound on how long URLs, even those used locally and with a custom scheme, can be.
i was comparing A string coming from XML with another String and results were showing that they are not equal. but in NSLog() both were same ( e.g. Valore Books ).
then i checked the Source of the XML and i came to know that the actual string is "Valore Books" and is infact a space. but the problem is this when i am comparing it with #"Valore Books", it is saying both are not same.
What to Do ??
Note: I'm replacing my original answer with one that's actually correct for this problem. Sorry for the initial misunderstanding.
The following line will unescape the html entities in your string.
NSString *A = #"Valore Books";
NSString *B = (NSString *)CFXMLCreateStringByUnescapingEntities(NULL, (CFStringRef)A, NULL);
I couldn't find any equivalent function that was higher level, but the performance of this should be excellent. If I read the docs correctly, you can pass in a CFDictionaryRef as the third argument to specify extra conversions, but it seems that this does a good job doing standard ones on it's own.
Docs are here.
Note that it's probably a good idea to handle the encoding whereever you're pulling those strings into your program at, and not everytime you're comparing.
Also found a second part of this you need to consider.   isn't just a space, it's a non breaking space, which the above code converts to \312 instead of the standard space. Those are in fact separate characters in the encoding and when you do a string compare it will fail.
Maybe it'd be easiest to replace #160 with #32 using
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement
and then running it through the unescape.
It also just occurred to me that the CFXMLCreateStringByUnescapingEntities won't be available on the iphone. Here is a link to an example that shows how to do similar conversions on the iphone.
is a non-breaking space (Unicode value U+00A0)
The regular space is (in #"Valore Books") has Unicode value U+0020.
So it is not the same character, and the two strings are not equal.
i solved the problem using a string parsing utility provided by google. Thanks everybody.
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!