Can't get XML element with colon in the name using TouchXML - iphone

I have the following code:
NSString *subtitle = [[[node elementsForName:#"subtitle"] objectAtIndex:0] stringValue];
NSString *duration = [[[node elementsForName:#"itunes:duration"] objectAtIndex:0] stringValue];
The first line works perfectly. The second line though won't work. I assume it has something to do with namespaces, but I'm pretty new to all of this so I would appreciate any guidance. Thank you!
It turns out that I can use the elementsForLocalName:URI: to read the element correctly. Now the problem is that since I am using the TouchXML library, it doesn't seem like that method has been mapped over to the CXMLElement structure (see here).
So the question now is: how can I convert a CXMLElement to an NSXMLElement so that I can use that method?

"itunes" is the namespace identifier. It doesn't actually have any significance on its own, it just links a URI with the element in question. It looks like you're using the iTunes RSS extensions, which live under the namespace http://www.itunes.com/dtds/podcast-1.0.dtd.
So, for namespaced elements, I think (I'm not familiar with Objective-C or NSXML :P) you want to use elementsForLocalName instead:
[node elementsForLocalName: #"duration" URI: #"http://www.itunes.com/dtds/podcast-1.0.dtd"]
For the answer to the second question, see comments below.

Related

Parse XML in iPhone (attributed not separated)

Is there a way to parse an XML in iOS where the attribute are not separated
e.g:
Users
UserId="1" Name="John Smith" Loc="London"
UserId="2" Name="Johnny Cash" Loc="Nashville"
Users
Thanks
It seams like you havent got xml at all. You are missing all usefully symbols that would normally help with the parsing. You taks is to parse a new format specification.
My first bit of advice is to ask whoever is providing you with this feed to put it into a proper format (JSON or plist are the easiest to work with).
Failing this, if the feed is not too big (otherwise you will hit performance issues), parse the feed manually character by character. You probably want to write a event based parser.
Split the feed line by line, perhaps using componentsSeparatedByString:
Then read characters into a string untill you hit an = that string is your key. Next read between the quotes "" That string is your value. FIre the key and the value off to a delegate.
JSON parsing classes will help you out...
NSString *responseString = #""; // your data contained string.
SBJSON *json = [[SBJSON new] autorelease];
NSArray *resultData = [json objectWithString:responseString error:&error];

Having trouble understanding how to work TBXML

I followed the TBXML guide and it's been successfully installed into my code, but the guide they have doesn't make sense to me. I want to get some values from an XML document. An example they have of starting this process is:
TBXML * tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:#"http://www.w3schools.com/XML/note.xml"]] retain];
In NSLog, for me this returns:
<TBXML: 0x4e3cc90>
This example XML file contains the following:
<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>
Could somebody give me a quick example on how from this XML file i would be able to extract the <body> of this? Their guide does seem fairly straight forward looking at it, but I just can't seem to make sense of it.
http://www.tbxml.co.uk/TBXML/Guides_-_Loading_an_XML_document.html is their guide.
Andrew,
It appears that once you init TBXML with the xml file, as it appears you have, you then 'traverse' elements in the document using various API. I haven't tested this but it would appear in your example that "body" is a child of "note", therefore... first get the note element and from the root element and extract the body element from the note element.
TBXMLElement *noteElement = [TBXML childElementNamed:#"note" parentElement:rootXMLElement];
TBXMLElement *bodyElement = [TBXML childElementNamed:#"body" parentElement:noteElement];
You should be able to traverse on anything at this point.
-- Frank

Sending array through email on iphone

Hey guys I need to send the content of an NSMutableArray through email. I have a mailing function but I'm not sure on how to place the content of the array into a NSString to be displayed on the email under each other. Is there a way to place all the content of the array into the string with maybe HTML nextline command between each array element?
NSString *emailBody = #"Need to put the body here";
Thanks
I'll suggest you convert your array into a text string using JSON. Then place the text in the email, send it away and use JSON on the receiving end to reconstruct the array.
You can get an iPhone version of JSON called TouchJSON here.
Claus
This process is known as serialization. Apple has a guide for it, that's worth reading through.
The simplest way is to call the array's description method which will return a human readable plist in a NSString.
If you need to reconstitute the array from the email. You will need save the array as xml plist using the writeToFile: method. Then read the file back in as a string. To reconstitute you will need to extract the xml from the email, put it in a NSString, write that to file, then read it back into an NSArray.
(IIRC, there used to be a way to write to NSString as if it was a file but I can't remember how to do it anymore. Probably, writing to a NSFileHandle and reading it back instantly.)
Edit:
Can you please explain more on the
array's description method please.
Like so:
NSArray *myArray=[NSArray arrayWithObjects:#"Obj1",#"Obj2",#"Obj3",nil];
NSLog(#"myArray=%#",[myArray description]);
...prints:
myArray=(
Obj1,
Obj2,
Obj3
)
For your project you can do:
NSString *arrayString=[myArray description];
The is also a descriptionWithLocale that will print the array in different languages. I don't have a ready example for that. See NSArray, NSLocale and The Locales Programming Guide

get element value

I have a NSString like that? (in iPhone application)
NSString *xmlStr = "<?xml version=1.0 encoding=UTF-8>
<information>
<name>John</name>
<id>435346534</id>
<phone>045635456</phone>
<address>New York</address>
</information>"
How I can get elements value?
(Do i need convert to XML format and get elements value? or split string? any way please tell me?)
Thank you guys.
If you want to use split string, you can use tokenization of strings using "componentsSeparatedByString" method. This is a more cumbersome method of course, rather than the recommended XMLParser
To get the name.
NSArray *xmlStr_first_array = [xmlStr componentsSeparatedByString: #"<name>"];
NSString *xmlStr_split = [NSString stringWithFormat:#"%#",[xmlStr_first_array objectAtIndex:1]];
NSArray *xmlStr_second_array = [xmlStr_split componentsSeparatedByString: #"</name>"];
NSString *name = [NSString stringWithFormat:#"%#",[xmlStr_second_array objectAtIndex:0]];
The most obvious solution is to use an XML parser to retrieve the values from each element.
You could use the excellent TBXML for this task. It provides a really simple interface where it wouldn't take more than a few lines to retrieve the desired values. The drawback to using this small library is that it (as far as I know) loads the entire XML data into memory. In this particular case, however, that is not problem at all.
There's of course also the option of using the NSXMLParser, though this is an event-driven parser, and thus a bit less simple to use.
Your string is in xml format already and you need to parse it to retrieve data. There're several options available - for example you can use NSXMLParser class or libxml library.
Edit: XMLPerformance sample project shows how to use both approaches and compare their performance.

Has anyone used Buzz Andersen's Simple iPhone Keychain code?

You can find it here.
I'm trying to understand his sample code in order to write a simple program with that stores passwords into the keychain and retrieves them for login. Has anyone done this before? It would be most appreciated if I can see any examples you may have.
There's really no code to demonstrate, he lists both calls there.
Heres's an example, for what it is worth:
NSError *error;
[SFHFKeychainUtils storeUsername:#"fred" andPassword:#"mypassword123" forServiceName:#"myService" updateExisting:YES error:&error];
NSString *storedPassword = [SFHFKeychainUtils getPasswordForUsername:#"fred" andServiceName:#"myService" error:&error];
To other newbies that use this code as is and don't understand why it doesn't work. Notice that the ServiceName is different (First without capital - "myService" and second with capital: "MyService"), that's why it doesn't work. Make the ServiceName the same.