473,790 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I am getting crazy. Can't access XML content in Firefox.

Hello sirs,

I am trying to send a POST request to a webservice on the click of a
button. This will return me an XML document with a list of combo box
items.

The problem: in FIREFOX, when the get the XmlDocument from the
XmlHttpRequest object, I can't access its contents. I keep getting
empty strings and "null".

This is my code:

---

function GetComboBoxItem s(p_strType, p_strCode)
{
var objParameters = new Array();
objParameters[0] = p_strType;
objParameters[1] = p_strCode;

var objHttpRequest =
SendRequest("/MeuWebService/MyWebService.as mx/GetComboBoxItem s",
objParameters);

alert(objHttpRe quest.responseT ext); // alerts the XML doc (see
below)

/*

this is the XML document:

<?xml version="1.0" enconding="utf-8"?>
<ReturnDocument >
<ComboItem>
<Key>1</Key>
<Description> My first item</Description>
</ComboItem>
<ComboItem>
<Key>2</Key>
<Description> My second item</Description>
</ComboItem>
</ReturnDocument>

*/

alert(objHttpRe quest.responseX ML); // alerts '[object XMLDocument]'

var XmlDoc = objHttpRequest. responseXML;

var arrTemp =
XmlDoc.document Element.getElem entsByTagName(" ComboItem");

alert(arrTemp.l ength); // alerts '2'

// show contents (Firefox)
for(var i = 0; i < arrTemp.length; i++)
{
alert(arrTemp[i].childNodes[0].nodeValue); // alerts empty string
alert(arrTemp[i].childNodes[1].nodeValue); // alerts 'null'
}

/*
// show contents (IE) - works perfectly
for(var i = 0; i < arrTemp.length; i++)
{
alert(arrTemp[i].childNodes[0].text);
alert(arrTemp[i].childNodes[1].text);
alert(arrTemp[i].childNodes[2].text);
} */
}
---

I searched everywhere for a logic explanation, but couldn't find any.

In IE it works perfectly. What am I doing wrong?
TIA,
Leonardo

Jul 23 '05 #1
4 12231
VK
What is SendRequest() ?

I don't see this function in your code.
Or are you trying to use Microsoft C# SendRequest() on Mozilla?

Jul 23 '05 #2
VK,

SendRequest is a custom function.

function SendRequest(p_s trURL, p_arrParameters )
{
try
{
// Firefox
xmlRequest = new XMLHttpRequest( );
}
catch(ex)
{
try
{
//IE
xmlRequest = new ActiveXObject(" Microsoft.XMLHT TP");
}
catch(ex)
{
xmlRequest = false;
}
}

// POST request
xmlRequest.open ("POST", p_strURL, false);
xmlRequest.setR equestHeader("C ontent-Type",
"applicatio n/x-www-form-urlencoded");

var strParameters = "";
for(var strKey in p_arrParameters )
{
if(strParameter s == "")
{
strParameters += "p_arrParametro s=" +
p_arrParameters[strKey];
}
else
{
strParameters += "&p_arrParametr os=" +
p_arrParameters[strKey];
}
}

xmlRequest.send (strParameters) ;

return xmlRequest;
}

But.. I found out the problem!

When I do :

var arrTemp =
XmlDoc.document Element.getElem *entsByTagName( "ComboItem" );

I have arrTemp with 2 items. That's correct, I have 2 elements in my
XML.

Now, inside each of these "ComboItem" , I have 2 items ("Key" and
"Descriptio n") in Internet explorer and 5 items ("Key", "Descriptio n"
and others) in Firefox.

I still doesn't understand this difference, but at least now I can get
the contents that I need. I do a for loop checking for nodeName. If
it's "Key" or "Descriptio n" I get the nodeValue of the firstChild node.

Thanks for the help!

Jul 23 '05 #3


le**********@gm ail.com wrote:

<?xml version="1.0" enconding="utf-8"?>
<ReturnDocument >
<ComboItem>
<Key>1</Key>
<Description> My first item</Description>
</ComboItem>
<ComboItem>
<Key>2</Key>
<Description> My second item</Description>
</ComboItem>
</ReturnDocument>

*/

alert(objHttpRe quest.responseX ML); // alerts '[object XMLDocument]'

var XmlDoc = objHttpRequest. responseXML;

var arrTemp =
XmlDoc.document Element.getElem entsByTagName(" ComboItem");

alert(arrTemp.l ength); // alerts '2'
So you can access the contents.
alert(arrTemp[i].childNodes[0].nodeValue); // alerts empty string
The first child node is a text node with whitespace, so that is what you
see.
alert(arrTemp[i].childNodes[1].nodeValue); // alerts 'null'


The second child nodes is the <Key> element node, element nodes do not
have a helpful nodeValue, so the null is all you get.

Instead of accessing the childNodes where you can get all kind of nodes
like text nodes, comment nodes, and that differs between browsers or
parser settings I strongly suggest to use getElementsByTa gName if you
are looking for an element e.g.
var comboItems =
objHttpRequest. responseXML.doc umentElement.ge tElementsByTagN ame('ComboItem' );
for (var i = 0; i < comboItems.leng th; i++) {
var comboItem = comboItems[i];
var key = comboItem.getEl ementsByTagName ('Key').item(0) ;
var description =
comboItem.getEl ementsByTagName ('Description') .item(0);
}
Now you have the element nodes and need to decide what information you
want, as you have seen MSXML in IE gives the whole text content with the
text property; Firefox has a similar property named textContent but
earlier versions of Mozilla do not implement that property; Opera so far
does not implement it either so in my view on the web it is best to
write a short helper function to collect text information e.g.

function getInnerText (node) {
if (typeof node.textConten t != 'undefined') {
return node.textConten t;
}
else if (typeof node.innerText != 'undefined') {
return node.innerText;
}
else if (typeof node.text != 'undefined') {
return node.text;
}
else {
switch (node.nodeType) {
case 3:
case 4:
return node.nodeValue;
break;
case 1:
case 11:
var innerText = '';
for (var i = 0; i < node.childNodes .length; i++) {
innerText += getInnerText(no de.childNodes[i]);
}
return innerText;
break;
default:
return '';
}
}
}

Then in the above for loop you could use

var comboItem = comboItems[i];
var key = comboItem.getEl ementsByTagName ('Key').item(0) ;
if (key) {
alert('Key has content: ' + getInnerText(ke y));
}
var description =
comboItem.getEl ementsByTagName ('Description') .item(0);
if (description) {
alert('Descript ion has content: ' + getInnerText(de scription));
}

and that is going to work with all Mozilla browsers, with IE/Win using
MSXML, with Opera 8.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #4
Thanks a lot Martin!

You saved my sanity :)

[]s

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2685
by: Gnolen | last post by:
Hi, I am really getting crazy here! I just do not get why this happens with the borders of the td/tr! I just want a border on the bottom of the rows(or td) but I just can't do it!!! I have tried so many different ways but I just thought this would do it: td {border-bottom: #000000 1px solid;} But no! What am I doing wrong? Because I can have a border of a td or tr in FF, right?! Thankful for any help here! Table and CSS is below..
2
11258
by: SACHIN | last post by:
I have this class as part of a Consol application. using System; namespace Bugreport { /// <summary> /// This class tries to use the Class/Struct combination. /// </summary> class Class1 {
10
6048
by: MHenry | last post by:
Hi, We were going merrily along for 6 years using this database to record all client checks that came into our office, including information about what the checks were for. Suddenly, network computers cannot access the database. The message is...
2
3959
by: Stan | last post by:
This is how I access Pieces field in my editable datargid during Update event: protected void grdMain_OnUpdate(Object sender, DataGridCommandEventArgs e) { string Pieces = ((TextBox) e.Item.FindControl ("txtPieces")).Text; .... }
4
1426
by: phil1bruening | last post by:
Hello! I need to access content page controls from within the master page. How can I do that??
2
5292
by: Praveen | last post by:
Hi All, I have made a webservice in C# and it works fine in my machine. I ran into a crazy problem when I wanted to deploy it in windows 2003 server. I have run "aspnet_regiis.exe -i" to make sure that the extensions for .asmx file etc are in place. I am getting http 404 when I give the url for the asmx file. the http error code is wrong because I am dead sure that the file is there. could you please let me know what else needs to be...
15
3086
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248: 'base::~base' : cannot access protected member declared in
6
9161
by: =?Utf-8?B?SmF5IFBvbmR5?= | last post by:
I am trying to access a Public property on a Master Page from a Base Page. On the content pages I have the MasterType Directive set up as follows: <%@ MasterType virtualpath="~/Master.master" %> On the Master Page I have a public property exposed: Public Property ErrorMessage() As String Get Return txtError.InnerText End Get
2
8107
ThatThatGuy
by: ThatThatGuy | last post by:
There's this aspx page proabably a content page.... There's a provision where i need to access a textbox (ASP or HTML Server) value can't access it in content page even if i do var str=document.getElementById("TextBox1").value; or even if it's inside a form var str=form1.TextBox1.value;
0
9512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9986
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7530
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5422
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.