XML to NSObject to HTML at runtime!

Discussion in 'iOS Programming' started by Sam77, Aug 18, 2010.

  1. Sam77 macrumors newbie

    Aug 17, 2010
    Hi. I want to be able to get data from an XML file and get it into an objects array. This i am able to achieve. After which, How can I modify it so that I generate an HTML file at runtime.

    for example, if this is the XML file:
    <detail>Detailed strings here, could be very long</detail>
    <detail>more details here of variable length</detail>
    After getting these two sections as two objects in an array, I want to dump it like an html file.
    <img src="image1.png"/>
    <h4>Detailed strings here, could be very long</h4>
    all those html tags are predefined in a css file.
    Similarly with the second object.

    How do i add new html tags to the objects from the array and add it to file in an "end of file" way.
  2. Cromulent macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    You might find XSLT, XPath and XQuery all offer what you want without the hassle of the intermediate step.
  3. Sam77 thread starter macrumors newbie

    Aug 17, 2010
    Thanks for the help, I searched around for related info on XSLT, Xquery, but found little info related to implementation on iphone.

    Apparently, XSLT is a headache on iphone.

    My basic problem is to find a library that Transforms the XML data into readable HTML page with html tags supported by a preset css.

    Could u explain a little more?
  4. chown33 macrumors 604

    Aug 9, 2009
    What are the types of the objects in the array?

    What does the actual detail data look like? Post a real sample, not a placeholder like "Detailed strings here". In particular, are there XML or HTML tags in the <detail> element?

    Is this all the data?

    Given the data you've presented, this looks like a fairly simple substring replacement:
    section -> p
    detail -> h4

    The most complex replacement is:
    into this:
    [COLOR="Red"]<img src="[/COLOR]image1.png[COLOR="blue"]"/>[/COLOR]
    However, you can replace the first red substring with the second red one, and likewise with the blue.

    This is the simplest thing that seems like it could work, but without knowing the full extent of the data, only you can tell.
  5. plinden macrumors 68040


    Apr 8, 2004
    Do you have any control over the data file?

    If so, I would take a look at using JSON and (if necessary) the TouchJSON library (http://github.com/schwa/touchJSON) instead. Your data would be in a smaller file and look something like (I may have made typos):
    "sections": [{
            "image": "image1.png",
            "detail": "Detailed strings here, could be very long"
            "image": "image2.png",
            "detail": "Detailed strings here, could be very long"
    JSON lists can be easily iterated over either in your code using TouchJSON or in a web view using Javascript. In fact, if you use Javascript, you can skip the middle step completely and just pass the JSON file in.

    If you'd prefer XML, I would suggest using a PLIST instead, since this can be easily parsed and manipulated using NSPreferences.
  6. Sam77 thread starter macrumors newbie

    Aug 17, 2010
    Thanks for your replies :)

    @chown33: you've got what I really want. perhaps a simple string replace is the answer.

    @plinden: Im reading about TouchJSON right now.

    I'll tell you about the data.

    Currently, I'm dealing with a simple TableView with datasource from a simple NSArray with NSStrings (These strings come from sqlite db). Its pretty straight forward. No problems at all.
    SQL Call returns this string:
    @"THis is string 1;This is string two;This is string 3 and all these can be of variable length";
    As you can see, string is delimited with a semi-colon ";" which i use to simply return it as an array. Hence, I've got an array with count = 3.

    Each object in the returned array solely contains ONLY a string. = (1 NSString)

    Now, I'd like to expand/complicate stuff, to make it display Images, side notes, other texts APART from the current data.

    In other words, Each object in the NEW Array should have Images (can be varying in number), A side text, and the current text. = (2 NSStrings, 1 or More Image).

    To make things simple, I thought of completely abandoning the SQL db storage and jump to XML. and the outlet would be a UIWebView rather than the previous UITableView.

    Also, to keep my future sane, I chose XML to preserve crossplatform usage of same data. Web, iOS utilising same xml files.

    The reason of choosing WebView was Automation, basically, I couldnt code a good enough tableView cell and auto realigns stuff when perhaps an object is absent.

    Heres what I am about to try out. convert the data from simple delimited texts to XML files with addition of images, subtexts etc.

    <title>THis is string 1</title>
    <subtext>New Text Added</subtext>
    <title>This is string two</title>
    <subtext>New text added here.. variable length</subtext>
    <note>Another kinda string</note>
    //Notice this section has no IMAGE!
    <title>This is string 3 and all these can be of variable length</title>
    //Notice there are no subtexts, notes here but two images
    Thats basically the format of the data I have currently in mind.
    I want to use this type to keep it readable and perhaps use it in a different platform in the future without issues.

    It'll be a pain though converting the delimited texts of my db into large files like these.

    And as chown33 mentioned, perhaps i'll simply add this data type to an html template with "replaced" html tags and use a UIWebView.

    Thanks for reading all this, im jus tired now.. ANY more suggestions are great
  7. chown33 macrumors 604

    Aug 9, 2009
    Sounds like you chose a data format (XML) without evaluating the actual ability to parse it as needed on a platform.

    JSON is a simpler data format that can easily cover everything you've presented so far in your data examples. It has parsers and generators on a wide variety of platforms and languages:
    And there are a lot more parsers and generators around than those.

    I can't help wondering what the database is, such that the whole thing needs to reside in its XML (or whatever) format on each device using it. Why can't you put the database on a web server, and retrieve the data with REST requests over HTTP. One of the parameters in the request could indicate what format to reply in: XML, JSON, plist, CSV, etc. The server does the heavy work, including data format translation. The client just shows what it gets.

    I'm also wondering why you don't pick a single master format, say XML, then process that on a more powerful platform to produce other formats, such as JSON or plist. One thing XML has over JSON is XSLT. Its output can even be another XML schema, such as XML-plist, which iOS should be able to use as-is. Again, your data structure is well within the capabilities of a plist.

    It seems like more explanation might be useful.
  8. Cromulent macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    The iPhone 4.0 SDK (and probably earlier) has libxml2 and libxslt in it already so you have everything you need to use XSLT without having to muck about with any extra libraries.

    That does mean you'll need to wrap the C stuff in Objective-C classes yourself, but that should be pretty easy; or you can keep it as plain C if you want. Either method would result in the same outcome.

    On the Mac it is slightly easier as Apple provide a fuller featured set of XML and XSLT Objective-C classes for that.

    The kind of transformation you are talking about is extremely easy.

    Read these articles for more information:



Share This Page