Solved

loadXML in FF

Posted on 2006-07-19
4
980 Views
Last Modified: 2008-01-09
Hi Experts,

I have a problem with the following code. The problem is that code works in IE and doesn't work in FF.
Please advice.

Thanks in advance

<HTML>
<HEAD>
<TITLE></TITLE>
<script>
function getXMLDocument()
      {
            var xDoc=null;
            if (document.implementation
                              && document.implementation.createDocument)
            {
                  xDoc=document.implementation.createDocument("","",null);
            }
            else if (typeof ActiveXObject != "undefined")
            {
                  var msXmlAx=null;
                  try
                  {
                              msXmlAx=new ActiveXObject("Msxml2.DOMDocument");
                  }
                  catch (e)
                  {
                        msXmlAx=new ActiveXObject("Msxml.DOMDocument");
                  }
                  xDoc=msXmlAx;
            }
            if (xDoc==null || typeof xDoc.load=="undefined")
            {
                  xDoc=null;
            }
            return xDoc;
      }

var txt = "<ruleValues><rule_value><value>v 1</value><name>value 1</name></rule_value><rule_value><value>v 2</value><name>value 2</name></rule_value></ruleValues>";
function test()

{
      xmldoc = getXMLDocument();
      //xmldoc.load("xml.xml");      
      xmldoc.loadXML(txt);
      ruleValues = xmldoc.getElementsByTagName("ruleValues");
      ruleValueNodes = ruleValues(0).childNodes;
      for (i = 0;  i < ruleValueNodes.length; i++)
      {
            valueNode = ruleValueNodes(i);
            id =  valueNode.childNodes(0).text;
            value =  valueNode.childNodes(1).text;
            alert("ID: " + id + " value: " + value);
      }      
}
</script>
</HEAD>
<BODY>
<input type="button" value="Test" onClick="test()">
</BODY>
</HTML>
0
Comment
Question by:RozanaZ
  • 3
4 Comments
 
LVL 17

Accepted Solution

by:
smozgur earned 500 total points
ID: 17142792
Hi,

This is how I would solve the issue for Firefox:

function test(){
     xmldoc = getXMLDocument();
     if (window.ActiveXObject){
          xmldoc.loadXML(txt);
     }else if(document.implementation.createDocument){
          var vParser = new DOMParser();
          xmldoc = vParser.parseFromString(txt, "text/xml");
     }

     ruleValues = xmldoc.getElementsByTagName("ruleValues");
     ruleValueNodes = ruleValues[0].childNodes;

     for (i = 0;  i < ruleValueNodes.length; i++){
          id =  ruleValueNodes[i].childNodes[0].childNodes[0].nodeValue;
          value =  ruleValueNodes[i].childNodes[1].childNodes[0].nodeValue;
          alert("ID: " + id + " value: " + value);
     }    
}

Simply; ruleValueNode includes 2 tags, "value" and "name": these tags include a single element, a text node (text content - childNodes[0]). Then nodeValue returns the this node's value that we can get by using .text property in IE but FF. So we used nodeValue in the sample code.

I hope it helps.

Suat
0
 
LVL 17

Expert Comment

by:smozgur
ID: 17142805
You can see following page for retrieving XML Data for Firefox and differences between IE and Firefox.
http://www.webreference.com/programming/prof_ajax/3.html

Suat
0
 
LVL 8

Author Comment

by:RozanaZ
ID: 17142823
Just perfect!
Thanks a lot!
0
 
LVL 17

Expert Comment

by:smozgur
ID: 17142833
You're welcome!

Thanks for the A grade!
Suat
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now