Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
New Developer
yreik
Posts: 5
Registered: ‎01-16-2009
My Device: Not Specified

Re: How to render xml on browser?

Here is the code that I am using --

 

Javascript that sends the request:

***********************************************************

var qry = 'param1=x&param2=y';
if (browser.isIE) {
    request = new ActiveXObject("Microsoft.XMLHTTP");
}
else {
    request = new XMLHttpRequest();
}
 
var url = "newPage.jsp";
request.open("POST", url, false);
request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
request.send(qryPrm);

var rspElm = request.responseXML.childNodes[1];

var firstElm = rspElm.childNodes[1].nodeName;
if (firstElm == "error") {
    //Do something
} else if (firstElm == "multiple") {
    //Do something else
}

var info = new Object();

info.node1      = rspElm.childNodes[1].text;
info.node2  = rspElm.childNodes[2].text;
//There are other nodes as well which I am omitting from this code snippet
***********************************************************

 

newPage.jsp (page that serves the response):
***********************************************************
//response.setContentType("text/xml");
response.setContentType("application/xhtml+xml");
response.setHeader("Pragme","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);

//Logic to calculate the node values

//Create the xml
<?xml version="1.0" encoding="utf-8" ?>
<response>
    <node1><%=node1%></node1>
    <node2><%=node2%></node2>
</response>

***********************************************************

Developer
Posts: 107
Registered: ‎09-25-2008
My Device: Not Specified

Re: How to render xml on browser?

A few things:
- You should probably be using responseXML.childNodes[0] instead of responseXML.childNodes[1]. The child nodes array is zero-indexed, and childNodes[0] should give you the root element. If that doesn't work try with responseXML.documentElement.
- If the above doesn't work, can you print out the .responseText property and post it here? You might be generating invalid XML which would then cause a null return value from .responseXML
- This is unrelated, but the HTTP pragma header is "Pragma" not "Pragme" :smileyhappy:
Developer
Posts: 107
Registered: ‎09-25-2008
My Device: Not Specified

Re: How to render xml on browser?

Oh, also, in general using childNodes might give you different results in different browsers because of whitespace nodes. It's safer to use getElementsByTagName('foo')[0] or something. If you want to use childNodes, you can check that the node you're accessing has a nodeType of 1 (element node) to make sure you're accessing what you think you're accessing.
New Developer
iGanja
Posts: 6
Registered: ‎01-03-2009
My Device: Not Specified

Re: How to render xml on browser?

staktrace is exactly right here. you cannot rely on the node indexes especially in mozilla browsers like firefox that add extra nodes for everything under the sun. you are better off using getElementsByTagName('nodename') to get to the nodes you need. and yes the node index is 0 based.

 

i'm not sure about the response.XML needing to be valid though. i'm pretty sure it doesn't, but if you are getting text with response.Text and not with response.XML, that may be something to look at. it looks like your xml is valid to me. at least what you claim to be sending. still until you see it on the client, you won't know.

 

yes, post your response.Text output.

 

 

New Developer
yreik
Posts: 5
Registered: ‎01-16-2009
My Device: Not Specified

Re: How to render xml on browser?

Thanks to both of you for your responses. And sorry for the delay at my end.

Actually this is an existing piece of code which was being used and our application was so far being used only on IE, so the code used to work. But I guess I will change it now since we need to deploy the app on Blackberry handsets.

The reason why node index [1] is being accessed (and not 0) is because in case of an erroneous situation or in case there are mutliple results, the application is populating the 1st node as either <errorMsg> or <multiple>.

 

Here is what I am seeing in the IE and Blackberry browsers:

When I do, alert(request.responseText); in both the browsers, the xml gets printed but with a lot of whitespace in the beginning. Here is the xml:

 






<?xml version="1.0" encoding="utf-8" ?>
<responseMsg>
<node1>1234</node1>
<node2>12345</node2>
<node3>ABC</node3>
<node4>ABCDE</node4>
<node5>USA</node5>

</responseMsg>

 

It gets printed in the same way in both the browsers, with all the whitespace in the beginning.

 

In the next statement, when I do alert(requester.responseXML):

in IE -- it shows no value

in Blackberry -- it shows null

 

And in the next statement if I try to do alert("node 0 is" + request.responseXML.getElementsByTagName('node1')[0].text), I get the value as 1234 in IE but in Blackberry it gives me this error message: Cannot convert 'null' to an object.

 

 

New Developer
iGanja
Posts: 6
Registered: ‎01-03-2009
My Device: Not Specified

Re: How to render xml on browser?

In order for your XML to be "well-formed" or "valid", the line:

<?xml version="1.0" encoding="utf-8" ?>
MUST be the first line of the "file" or stream in this case.

if you have white space before it, you need to go back to your server code and remove that.

 

it's not too surprising that you can parse the stream in IE, but it doesn't show in the alert.

you must have a lot of whitespace "characters" at the beginning as IE will truncate messages in alert boxes, so my guess is you are just seeing the whitespace.

 

If the blackberry browser requires the stream to be "valid" or at least "well-formed" (good bet,) this could very well be your problem.

New Developer
francis_mk
Posts: 1
Registered: ‎05-28-2009
My Device: Not Specified

Re: How to render xml on browser?

[ Edited ]

I think that this post isnt solve up to now. So heres my two cents.

 

Working code to access the value is:  "request.responseXML.getElementsByTagName('node1')[0].childNodes[0].nodeValue;"

 

Explaination and confusion hehe:

 

I think the inner text or node value is considered as a separate element but I dont know why maybe its a bug or just the way it is designed.

 

It took me a good amount of  time yesterday to figure this out.

Message Edited by francis_mk on 05-29-2009 01:05 AM
Guru I
Xandrex
Posts: 18,406
Registered: ‎07-29-2008
My Device: Z30, Playbook, 9320BES
My Carrier: B&You _ SFR

Re: How to render xml on browser?

in HTML, there are tag nodes like A, TABLE, TR, HR, and so on.
and there are also TextNodes like "this is a text node".

so when you write < a href="toto.html" > lala < /a >

then A is a DOM element and "lala" is a TextNode.



The search box on top-right of this page is your true friend, and the public Knowledge Base too: