Hiya - I am really struggling here - I want to load an XML document in objective-c and loop each node and encrypt the string value of that node. Then encryption bit i have sorted but when i run the following code it just changes the root node and every other node and not the values of the nodes.
e.g. - i want to go from:
<root>
<node1>some text</node1>
<node2>
<node3>some more text</node3>
</node2>
</root>
to something like:
<root>
<node1>encrypted text here</node1>
<node2>
<node3>encrypted text here</node3>
</node2>
</root>
but my code below just returns:
<root>
encrypted text here
</root>
Code is currently:
NSString *filenameStr = @"/Users/blah/Documents/project/filetestIN.xml";
//OPEN XML
NSXMLDocument *xmlDoc;
NSError *err=nil;
NSURL *furl = [NSURL fileURLWithPath:filenameStr];
if (!furl) {
NSLog(@"Can't create a URL from file %@.", filenameStr);
}
xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:furl options:(NSXMLNodePreserveWhitespace|NSXMLNodePreserveCDATA) error:&err];
if (xmlDoc == nil) {
xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:furl options:NSXMLDocumentTidyXML error:&err];
}
//ENCRYPT IT NODE BY NODE
StringEncryption *crypto = [[[StringEncryption alloc] init] autorelease];
NSArray *nodes = [xmlDoc nodesForXPath:@"//*" error:&err];
for (NSXMLElement *categoryElement in nodes) {
[crypto encryptXMLNode:categoryElement];
}
//SAVE XML
NSData *xmlData = [xmlDoc XMLData];
NSString *filenameStrOUT = @"/Users/blah/Documents/project/output/filetestOUT.xml";
NSURL *furlOUT = [NSURL fileURLWithPath:filenameStrOUT];
if (!furlOUT) {
NSLog(@"Can't create an URL from file %@.", filenameStrOUT);
}
[xmlData writeToURL:furlOUT atomically:YES];
//and the ecnrptionbit:
- (void)encryptXMLNode:(NSXMLNode *)node
{
if ([node kind] == NSXMLElementKind)
{
NSString *name = [node name];
NSString *strval = [node stringValue];
NSLog(@"old:%@=%@", name, strval);
//in time the actual encryption of strval will go here:
NSString *newVal = @"encrypted text here";
[node setStringValue:newVal];
name = [node name];
strval = [node stringValue];
NSLog(@"new:%@=%@", name, strval);
}
}
Many thanks for any help at all!