www.webdeveloper.com
Results 1 to 7 of 7

Thread: XML Parsing - IE vs Firefox

  1. #1
    Join Date
    Jan 2011
    Posts
    4

    XML Parsing - IE vs Firefox

    I am having a problem parsing an XML document in Firefox. My XML document is:
    <?xml version="1.0" encoding="UTF-8" ?>
    <data>
    <result>0</result>
    <totalCheers>1</totalCheers>
    <imagePath>../images/cheers!.png</imagePath>
    </data>

    When I get the XML back from the server, I use the following code to work with it:
    function handleRQ_CHEERS(xmlData){
    //get the data XML node
    intDataNodeIndex = xmlData.childNodes.length-1;

    if(xmlData.childNodes[intDataNodeIndex].childNodes[0].text == "0"){
    objImages = objCont.getElementsByTagName("img");
    objImage = objImages[0];
    objImage.src = strImagesDir + 'cheers!.png';

    objParagraphs = objCont.getElementsByTagName("p");
    objParagraph = objParagraphs[0];
    objParagraph.innerHTML = xmlData.childNodes[intDataNodeIndex].childNodes[1].text + ' cheers';
    }
    }

    When I access xmlData.childNodes[intDataNodeIndex].childNodes[0].text in IE it works just fine, in Firefox it return undefined as the text value.

    I have confirmed that the .tagName property is 'result' in both ie and firefox so I know both browsers are looking at the same element.

    Thanks in advance for any advice!

  2. #2
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,774

  3. #3
    Join Date
    Jan 2011
    Posts
    4
    Thank you for the reply. I tried using .nodeValue instead of .text and that did not fix the problem. It now returns a null..

  4. #4
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by spence_74 View Post
    Thank you for the reply. I tried using .nodeValue instead of .text and that did not fix the problem. It now returns a null..
    it's .textContent ; nodeValue is only for nodes, not elements.

  5. #5
    Join Date
    Jan 2011
    Posts
    4
    Using .textContent works like a charm with Firefox! Thank you very much!

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by rnd me View Post
    it's .textContent ; nodeValue is only for nodes, not elements.
    Correct. But considering the fact that a text is in fact a node as well (a textNode), I guess that nodeValue still can be used:
    Code:
    xmlData.childNodes[intDataNodeIndex].childNodes[0].childNodes[0].nodeValue
    data is also a property to be used:
    Code:
    xmlData.childNodes[intDataNodeIndex].childNodes[0].childNodes[0].data
    Of course, using textContent is simpler
    Last edited by Kor; 01-27-2011 at 04:49 AM.

  7. #7
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by Kor View Post
    Correct. But considering the fact that a text is in fact a node as well (a textNode), I guess that nodeValue still can be used:
    Code:
    xmlData.childNodes[intDataNodeIndex].childNodes[0].childNodes[0].nodeValue
    but if there is more than one text node inside the element (happens all the time), the above code would not grab all the tag's text using .childNodes[0] ...

    you can .normalize() the nodes, but how many people actually do that?
    the other thing to keep in mind is that IE and FF count child nodes differently...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles