XML responseType

5 views
Skip to first unread message

h8windows

unread,
Mar 30, 2008, 6:52:18 PM3/30/08
to CiUI
As to my earlier inquiry regarding retrieving xml from external
servers, here is what I've come up with. Comments regarding the
cleanliness and efficiency would be appreciated, as I consider myself
more of a hack than a programmer.

I know that all my target strings will contain loadxml.php, as that is
the server-side script that will make the request to the external data
source.

updatePage: function(target) {
...

else {
xmlString = new String(target);
xmlRE = new RegExp("loadxml.php");
results = xmlString.match(xmlRE);
backButtonTextEl.innerHTML =
(pageHistory[pageHistory.length-2]) ?
trim(pageHistory[pageHistory.length-2].title, 15) : "Back";
pageTitleEl.innerHTML = trim(target.title);
if (results == "loadxml.php"){
xmlOrText = "XML";
}else{
xmlOrText = "TEXT";
}
(new this.ajax(target.href, this.updatePageContent,
xmlOrText)).run();

Then, further down...

function getResponse() {
...
if (responseType == "XML") {
var data=httpRequest.responseXML;
var items=data.getElementsByTagName('current_observation');
var end=items.length;
var item,location,observation_time,weather,newContent='';
if(end<1){return;}
for(var i=0;i<end;i++){
location=items[i].getElementsByTagName('location').item(0).firstChild.nodeValue;
observation_time=items[i].getElementsByTagName('observation_time').item(0).firstChild.nodeValue;
weather=items[i].getElementsByTagName('weather').item(0).firstChild.nodeValue;
item='Location: <B>'+location+'</B>';
item2='<br>'+observation_time;
item3='<br>Current Conditions: '+weather;
newContent+=item;
newContent+=item2;
newContent+=item3;
}
responseConsumer(newContent);

The tag elements listed above are hard-coded for testing purposes.
I'll refine further to accommodate non-specific tags. The elementary
styling will also be changed to something slightly more elegant. I'm
thinking that if the above feed has more than one location tag I'll be
in trouble!

If anyone is interested, I can also post the loadxml.php code.

Regards,
-Chris

blueice

unread,
Mar 31, 2008, 8:13:15 PM3/31/08
to CiUI
i think what we need to do here is to add the ability to define your
own custom update code. basically, we need to define a handle on the
updatePageContent function. the only problem right now is that it
relies on some internal code so that will need to be separated out
first, which shouldn't be too hard.

so, when all done and working, you'll be able to do something like
this:

function myCustomContentHandlingFunction() {
// your code here
};

CiUI.updateContentFunct = myCustomContentHandlingFunction;

before making this change, we would probably also want to specify what
AJAX should return (text || XML). so the full configuration would look
like this:

CiUI.ajaxResponseType = "XML"; // could also be "TEXT"
CiUI.updateContentFunct = myCustomContentHandlingFunction;

this all could then be generalized into a single custom initialization
object, i.e.:

CiUI.Preferences = ({'ajaxResponseType' : 'XML',
'updateContentFunct' : myCustomContentHandlingFunction});

what do you guys think?

h8windows

unread,
Apr 1, 2008, 8:36:02 PM4/1/08
to CiUI
Wow. It looks like I write with a chainsaw while you use a scalpel.
Your solution certainly would allow users with varying requirements to
easily access the objects. The education I'm getting from this is more
valuable than the 5 or so books I've got on my shelf.

-Chris

FrankieShakes

unread,
Apr 2, 2008, 9:56:42 AM4/2/08
to CiUI
Hey Guys,

I figure I should chime in on this one as well, since this is exactly
what I had posted about in the original blog posting.

Vladimir,

It's funny, this is the very idea I was working on over the weekend.
To extend CiUI to support JSON responses, whereby specifying a
callback function to handle the response. I went the route of using a
"rel" attribute in the <a> tag:

<a href="myjsonfunction.php" rel="myJSONCallback" title="Page
Title">Item 1</a>

Where the myJSONCallback function is the the one that handles the
response for the specific request. Because different links may call
different server-side scripts, specifying the callback at the link
level seems like a good way to allow for de-coupling of CiUI and the
Application-specific functionality.

Any thoughts?
Frank

Vladimir Olexa

unread,
Apr 2, 2008, 7:09:24 PM4/2/08
to CiUI
hey frankie, while this is a great suggestion, i don't really think we
should mix HTML with Javascript. meaning, the link annotations should
be just that - they should describe the element and not refer to
external JS objects. so i think we should leave all internal CiUI
customization to your JS code, as i suggested previously.

i am open to hear counter-arguments, however.

FrankieShakes

unread,
Apr 5, 2008, 8:49:14 AM4/5/08
to CiUI
Hey Vladimir,

Thanks for the feedback. I had originally thought to use that since
it would be easier to have multiple handlers depending on the link
that was pressed. So you could have something like:

<li><a href="http://mysite.com/list_of_friends.json.php"
rel="processListOfFriends">List of Friends</a></li>
<li><a href="http://mysite.com/list_of_photos.json.php"
rel="processListOfPhotos">List of Photos</a></li>

Since the JSON coming back from the server needs it own custom
handler, I figured it would be easier to just refer to the function in
the "rel" attribute, rather than attaching an onclick handler to each
link and have that handler specify the "updateContentFunct" function
you had suggested.

Any idea on how we could maybe combine the two? Or are you thinking
something entirely different altogether?

Thanks again,
Frank
Reply all
Reply to author
Forward
0 new messages