Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Feb 2004
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Loading an XML file (either a data island or external) into a DOM

    I am having a really hard time in figuring out how to load an XML file into a DOM. I can get it done in IE, but in mozilla I am missing something. Here is what my loadXML() function looks like:
    Code:
    function loadXML(){
    	try {
    		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");	
    		xmlDoc.async = "false"; 
    	 	xmlDoc.onreadystatechange = verify;
    		hasFile = xmlDoc.load(info.XMLDocument);
    		if (hasFile){
    			xmlObj = xmlDoc.documentElement;
    			allTopics = xmlObj.getElementsByTagName("topic");
    		}
    	}
    	catch(e) {
    		xmlDoc = (new DOMParser()).parseFromString(document.getElementById('info').innerHTML, 'text/xml');
    		hasFile = true;
    		allTopics = xmlDoc.getElementsByTagName("topic");
    		
    		if (hasFile){
    			allTopics = xmlDoc.getElementsByTagName("topic");
    			alert(allTopics[0].firstChild.getAttribute("name"));
    		}
    	}
    }
    Where verify is another function. The try part works for IE, but the catch part doesn't work for Mozilla. I am not finding any information as to really use an XML DOM properly in Mozilla. I'm trying to get allTopics to be a handle on the same thing in both the "try" and the "catch"

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, this is a cross-post (http://codingforums.com/showthread.php?p=208596)...but perhaps this will be of service? Also, you don't have to load an XML document into an XML document (as you were doing with your ActiveX object).

    Hope that helps!

    Happy coding!

  • #3
    Regular Coder
    Join Date
    Feb 2004
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry about the cross-post but sometimes when you are working on a problem so long, you think it's been a long time since you posted, and you ask again, not remembering when you posted last. Anywho, I'll take a look at your code and see if I can make some sense of it. Thanks.

  • #4
    Regular Coder
    Join Date
    Feb 2004
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts
    nonlachrymose, I was wondering if you've seen other posts about using data islands in mozilla. A bunch of sites seem to think it is really easy and you are just suppose use the xmlDoc.load() function (where xmlDoc = document.implementation.createDocument("","",null).

    What mistake amy I or all these sites makeing. Obviously you've created something that you think works, so you've probably seen those sites too, straight from mozilla. Am I an idiot or are they just leaving something out?

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    My guess is you're loading a document correctly, but you are trying to access it before it has finished loading due to the fact that, by default, the XML document is loading asynchronously. Perhaps try this?

    Code:
    function loadXMLDocument(s) {
    	var xmldoc = document.implementation.createDocument('', '', null);
    	xmldoc.async = false;
    	xmldoc.load(s);
    	return xmldoc;
    }
    Hope that helps!

    Happy coding!

  • #6
    Regular Coder
    Join Date
    Feb 2004
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Man it's driving me crazy. That's not it either. I've changed my load function quite a bit and I have the following:
    Code:
    function loadXML(){
    	try {
    		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    		xmlDoc.async = "false"; 
    	 	xmlDoc.onreadystatechange = verify;
    		hasFile = xmlDoc.load("../xml/ca.xml");
    	
    		if (hasFile){
    			allTopics = xmlDoc.getElementsByTagName("topic");
    		}
    	}
    	catch(e) {
    	var xmlDoc = document.implementation.createDocument("", "", null);
    		hasFile = xmlDoc.load("../xml/ca.xml");
    		xmlDoc.async = false;
    		if (hasFile){
    			  var s = new XMLSerializer();
    			  var str = s.serializeToString(xmlDoc);
    			alert(str);
    		}
    	}
    }
    The try{} part works for IE, but for mozilla, the catch{} part does nothing, that is it gets as far as alert(str), which means hasFile = true; and that the document loaded, but str is an empty string, I know because I tested it with,
    alert('tester' + str), and the word "tester" came up, meaning the alert works and everything.

    The serialization isn't what I ultimately want, I was just using it as a test to see what xml file it was actually loading, and for whatever reason it isn't loading any. I used almost the exact same code (or rather somebody else did) and i ran it and it worked fine, using the same .xml file, accessed from teh same place. It makes no sense.

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You have to set it as synchronous before you load the document. However, if you just want the text of the document, there's no need to load an XML document and serialize it to a string; just use an XMLHttpRequest().

    Code:
    function loadXMLDocument() {
    	var p = new XMLHttpRequest();
    	p.overrideMimeType('text/xml');
    	p.open('GET', '../xml/ca.xml', false);
    	p.send('');
    	var xmldoc = p.responseText;
    	alert(xmldoc);
    }
    Hope that helps!

    Happy coding!

  • #8
    Regular Coder
    Join Date
    Feb 2004
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I was really just using the alert() to help me see where I was in the process, which was usually not very far.

    I mean in very few words I want this:
    Code:
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = "false";
    xmlDoc.onreadystatechange = verify;
    hasFile = xmlDoc.load(info.XMLDocument);
    	
    if (hasFile){
        allTopics = xmlDoc.getElementsByTagName("topic");
    }
    That is, I want that allTopics object, but instead of only being a microsoft proprietary object, I want one that works for mozilla, or rather is browser-neutral. I mean I've tried to create a document with document.implementation.createDocument("","",null), I've tried using DOMParser. I feel like I've tried everything but I just can't get an object that is exactly like the one I have created for IE browsers only. And I need an object exactly like that because the rest of my methods depend on it.

    By the way the line that has info.XMLDocument is a data island. If you've read my other posts (and errantly reposts), you probably know that already. I wanted to create an HTML file that was totally self-sufficient sort a speak.

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function getXMLDocument(xmlobj) {
    	if(typeof xmlobj.XMLDocument != 'undefined')
    		return xmlobj.XMLDocument;
    	else
    		return (new DOMParser()).parseFromString(xmlobj.innerHTML, 'text/xml');
    }
    xmlobj is a reference to the <xml> element.

    Hope that helps!

    Happy coding!


  •  

    Posting Permissions

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