PDA

View Full Version : XML to NSObject to HTML at runtime!




Sam77
Aug 18, 2010, 08:27 AM
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:
<section>
<image>image1.png</image>
<detail>Detailed strings here, could be very long</detail>
</section>

<section>
<image>image2.png</image>
<detail>more details here of variable length</detail>
</section>

After getting these two sections as two objects in an array, I want to dump it like an html file.
example:
<p>
<img src="image1.png"/>
<h4>Detailed strings here, could be very long</h4>
</p>
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.



Cromulent
Aug 18, 2010, 09:27 AM
You might find XSLT, XPath and XQuery all offer what you want without the hassle of the intermediate step.

Sam77
Aug 18, 2010, 10:44 AM
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?

chown33
Aug 18, 2010, 12:04 PM
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:
<image>image1.png</image>

into this:
<img src="image1.png"/>

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.

plinden
Aug 18, 2010, 12:21 PM
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": [{
"section":{
"image": "image1.png",
"detail": "Detailed strings here, could be very long"
},{
"section":{
"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.

Sam77
Aug 19, 2010, 03:21 PM
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.

<section>
<title>THis is string 1</title>
<subtext>New Text Added</subtext>
<image>image1.png</image>
</section>

<section>
<title>This is string two</title>
<subtext>New text added here.. variable length</subtext>
<note>Another kinda string</note>
</section>
//Notice this section has no IMAGE!

<section>
<title>This is string 3 and all these can be of variable length</title>
<image>image3.png</image>
<image>image4.png</image>
</section>
//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

chown33
Aug 19, 2010, 03:58 PM
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:
http://json.org/
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.

Cromulent
Aug 19, 2010, 04:42 PM
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?

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:

http://www.xfront.com/rescuing-xslt.html

http://www.w3schools.com/xsl/