Build XML Using NSString data in Xcode

Discussion in 'iOS Programming' started by ilx.mac, Mar 10, 2009.

  1. ilx.mac macrumors member

    Joined:
    Mar 10, 2009
    #1
    Hi Friends,

    I am building an iphone application. I need to build an xml from NSString Data. I am new to this iphone development. Can some one help me by sending / posting some sample codes to build an xml using NSString.

    Thanks in Advance!
     
  2. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #2
    Not quite sure how to answer this; XML is (within the limits of it being a structured document) fairly free-form, since you get to define the tags, element properties, etc. For instance,

    Code:
    [NSString stringWithFormat:@"<bacon>%@</bacon>", aString];
    is a perfectly valid way to construct XML from an NSString. Maybe you'd benefit from reading up a little on XML: http://en.wikipedia.org/wiki/XML and NSStrings: http://developer.apple.com/document...lasses/NSString_Class/Reference/NSString.html.
     
  3. ilx.mac thread starter macrumors member

    Joined:
    Mar 10, 2009
    #3
    Re

    Thanks for your prompt reply Jnic. I am getting a reply from web server as a NSString (*thereply). and the data from string thereply reads - <root><type>text</type></root>. Now will it be possible for me to get the values from the node as we do it in Java/.Net.

    Thanks in Advance!
     
  4. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
  5. ilx.mac thread starter macrumors member

    Joined:
    Mar 10, 2009
    #5
    Re

    Thanks Jnic. I think i can explain my problem in brief.

    #1. I have a webserver.
    #2. I have a client, which sends request to the webserver.
    #3. The webserver responds me with <root><name>Todd</name><age>25</age></root>.
    #4. The returned value is not an xml, its an string.
    #5. I have to build the string to an xml. (In c#, we would load the string to an xmldom object to convert to an xml). Here i am unable to do this.
    #6. After building an xml I have to parse it & retrive the node values.

    Can you help me on this? its very urgent for me.

    Thanks in Advance!
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    #2. I'm assuming your client is an iPhone or you wouldn't be in this forum.
    #4. "An XML". "A string". This is just a matter of perspective. And doesn't really matter. Your returned value is an XML document of some form.

    SO, as jnic said, you'll need to figure out how to use NSXMLParser to parse through the XML document that your request returns. A good place to see some sample code to learn about this is the SeismicXML sample app.
     
  7. ilx.mac thread starter macrumors member

    Joined:
    Mar 10, 2009
    #7
    Re

    thanks Dejo,

    I went through the code, but i cant find code to build a xml from string. Can you send me some code samples please!
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    If you're using NSXMLParser, you're not going to get a string as a response. In fact, it uses an event-driven approach where you need to provide code to handle things inside the didStartElement and didEndElement methods. And I believe NSXMLParser is the only XML-handling that's part of the API. So, if you aren't using this, you'll have to "roll your own", as they say.
     
  9. return7 macrumors 6502

    Joined:
    Oct 8, 2008
    #9
  10. roddev macrumors newbie

    Joined:
    Mar 19, 2014
    #10
    simple window with sample

    see attached zip file
    it's a window, with a text box, a label, and a button.
    input <xml><into>text box</into></xml>
    press the button.

    so it's xcode 5.1
    simple cocoa application, but should suffice in an ios app as well.
     

    Attached Files:

Share This Page