problem with reading XML-Document in Firefox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • giordan
    New Member
    • Dec 2007
    • 10

    problem with reading XML-Document in Firefox

    Hi,
    I'm new in this forum.
    I was searching for a solution to read xml file with javascript in Firefox (I hate this browser...grrrr r...) and I've found this topic.
    I've read all but my problem still exist! :-(

    I report what I've wrote. The original script I've made is more complicated but the problem is the same, so I've tried with a smaller page. That's the source:

    [code=javascript]<html>
    <head>
    <script type="text/javascript">
    var xml;
    function test(){
    // Load XML Explorer
    if(window.Activ eXObject){
    xml = new ActiveXObject(" Microsoft.XMLDO M");
    xml.async = false;
    xml.load("casa. xml");
    var prova = xml.selectSingl eNode("prodotti/prodotto/codice[text() = 0001]");
    var nome = prova.parentNod e.selectSingleN ode("titolo").t ext;alert(nome) ;
    }
    else{
    if(document.imp lementation && document.implem entation.create Document){
    xml = window.document .implementation .createDocument ("","",null) ;
    var caricamento = xml.load("casa. xml");
    xml.onload = readXML;
    //alert(xmlFile);
    }
    }
    }
    function readXML(){
    var toTry = xml.getElements ByTagName("prod otti")[0].childNodes[0].nodeValue;
    alert(toTry); // Result Empty Alert

    }

    </script>
    </head>
    <body onload="test()" >
    <div id="test">
    </div>
    </body>
    </html>[/code]

    I was trying to read from an XML file. In IE I have no problems, but Firefox don't want to read...
    Some one coult help me or I have to carry Firefox to Primary School to learn how to read???

    Thank you (and sorry for my English...I'm Italian...).

    Giordan.
    Last edited by pbmods; Dec 8 '07, 04:19 PM. Reason: Fixed CODE tags, split post into its own thread.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    i assume your script is trying to read the first linebreak or whitespace and that has no nodeValue ... so try to alert the nodeName and post what you get :) ... its not firefox that makes the problem ... its the xml-file ...

    kind regards

    Comment

    • drhowarddrfine
      Recognized Expert Expert
      • Sep 2006
      • 7434

      #3
      Please be careful you are using proper DOM methods, too. Internet Explorer is almost 10 years behind current web standards for the DOM while all other browsers, including Firefox, are fairly up to date. Don't trust IE to be doing this properly.

      Comment

      • rnd me
        Recognized Expert Contributor
        • Jun 2007
        • 427

        #4
        > selectsinglenod e does not work in firefox.

        ---

        my universal xml getter:

        Code:
        function IOx(U){
         var X=((!window.XMLHttpRequest)?new ActiveXObject('Microsoft.XMLHTTP'):new 	XMLHttpRequest() );
        	X.open('GET',U,false);
        	if(X.overrideMimeType) X.overrideMimeType( "text\/xml");
        	X.send('');	
              var rx=X.responseXML;
           if(rx) {return (rx.documentElement || rx ); };
        }//end iox
        IOx.txt="textContent"; if(document.all){IOx.txt="text"; }
        
        
        function test(fileURL){
            var resp=IOx(fileURL);
            var myNode resp.getElementsByTagName("prodotto")[0];
            alert( myNode[IOx.txt]  );
        }//end function test
        
        test("casa.xml");

        use node[IOx.txt] to grab text across browsers.

        Comment

        • giordan
          New Member
          • Dec 2007
          • 10

          #5
          Hi all,
          thank you for the speedy-answer :-)
          I've tried to ask the nodeName and Firefox answer me "#text".
          I try to explain what I've done (and work properly in IE):
          I have a shop, people can add what they like to a cart. When they end to shopping, going in Cart's page, they can buy what they have chosen.
          Every time they add something to the cart, a cookie was added. I know every problem due to cookies, but it's not this the problem ;-)
          When Cart's page is open, the cookies was read and, for every item, was opened the relative xml, found the product via id and extracted description and prize. All this values are memorized in many array. With a for loop was build via javascript a table with all values and put that in a empty div.

          Well...firefox do all this step right but when it try to read xml file go boooom!

          I post an example of xml that I use...it's nothing special...

          Code:
          <?xml version="1.0" encoding="windows-1252"?>
          <prodotti>
          	<prodotto disp="da ordinare">
          		<titolo>Spilla pesciolino</titolo>
          		<codice sezione="spille">0022</codice>
          		<immagine>http://www.naera.it/public/images/Shop/Spille/Spilla_pesciolino.jpg</immagine>
          		<ingrandimento>http://www.naera.it/public/images/Shop/Spille_Originali/Spilla_pesciolino.jpg</ingrandimento>
          		<descrizione>Spilla in cernit con pesciolino. Fornita di chiusura di sicurezza.</descrizione>
          		<prezzo>3,00€</prezzo>
          	</prodotto>
          </prodotti>
          In the script I've posted I'm only trying to exctract "prodotti" node...

          The steps I would do are:

          1) found item whit XXXX code
          2) go to parent node
          3) select description and memorize the value in an array
          4) select prize and memorize the value in an array

          I thanks who posted XMLHTTPRequest method but...I don't know HTTPRequest and I think I spend much more time with that...
          I've read that it's possible with alternative method...

          Please help me...it's 3 days that I'm crashing my head to the wall!!!

          Comment

          • giordan
            New Member
            • Dec 2007
            • 10

            #6
            Ok guys,
            I've tried the XMLHTTPRequest method and it's near to work.
            In Firefox work if the url isn't absolute, in IE work if it's absolute.
            Code:
            <html>
            <head>
            <script type="text/javascript">
            	function IOx(U){
             var X=((!window.XMLHttpRequest)?new ActiveXObject('Microsoft.XMLHTTP'):new     XMLHttpRequest() );
                X.open('GET',U,false);
                if(X.overrideMimeType) X.overrideMimeType( "text\/xml");
                X.send(''); 
                  var rx=X.responseXML;
               if(rx) {return (rx.documentElement || rx ); };
            }//end iox
            IOx.txt="textContent"; if(document.all){IOx.txt="text"; }
             
             
            function test(fileURL){
            		var nCount = 0;
                var resp=IOx(fileURL);
               while(resp.getElementsByTagName("prodotto")[nCount]){
                		var myNode = resp.getElementsByTagName("prodotto")[nCount].getElementsByTagName("codice")[0];
                		if(myNode[IOx.txt] == 0013){alert("entro");
                			var myName = resp.getElementsByTagName("prodotto")[nCount].getElementsByTagName("titolo")[0];
                			var nome = myName[IOx.txt];
                			break;
                		}
                		else{
                			alert('aggiungo');
                			nCount++;
                		}
                		
                	}
                  alert( nome  );
            }//end function test
             
            test("http://www.naera.it/public/xml/casa.xml"); //if try 'test("casa.xml")' it work properly.
            	</script>
            </head>
            I've tried to put some 'alert' and I discovery that the problem is X.open
            Before this line I have the alert, after no.

            Someone could help me?

            Thank you.

            Comment

            • giordan
              New Member
              • Dec 2007
              • 10

              #7
              Hey!
              Good news!
              I've done what I wanted!
              The problem?
              This line:
              Code:
              IOx.txt="textContent"; if(document.all){IOx.txt="text"; }
              Must become:
              Code:
              IOx.txt="textContent"; if([B]window.[/B]document.all){IOx.txt="text"; }
              Thank you all!

              Comment

              • drhowarddrfine
                Recognized Expert Expert
                • Sep 2006
                • 7434

                #8
                Do not use document.all .

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Originally posted by giordan
                  I was searching for a solution to read xml file with javascript in Firefox (I hate this browser...grrrr r...)
                  Oh wow! Most developers hold that opinion of IE. IE has a lot of bugs and quirks. Usually, if you have problems with reading XML in Firefox, you've coded for IE only and forgotten to account for whitespace when parsing.

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5390

                    #10
                    yep ... as i said :) ... try to alert the nextSibling's nodeValue in FF ... i guess its the typical whitespace/linebreak-problem ... the xml-document shouldn't contain whitespaces and/or linebreaks ...

                    kind regards

                    Comment

                    Working...