...

View Full Version : Loading an XML file (either a data island or external) into a DOM



deadseasquirrel
06-09-2004, 06:37 PM
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:


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"

nolachrymose
06-09-2004, 07:21 PM
Well, this is a cross-post (http://codingforums.com/showthread.php?p=208596)...but perhaps this (http://codingforums.com/showthread.php?p=208443) 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! :)

deadseasquirrel
06-10-2004, 02:43 PM
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.

deadseasquirrel
06-10-2004, 02:48 PM
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?

nolachrymose
06-10-2004, 06:12 PM
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?


function loadXMLDocument(s) {
var xmldoc = document.implementation.createDocument('', '', null);
xmldoc.async = false;
xmldoc.load(s);
return xmldoc;
}

Hope that helps!

Happy coding! :)

deadseasquirrel
06-10-2004, 07:39 PM
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:


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.

nolachrymose
06-10-2004, 11:10 PM
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().


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! :)

deadseasquirrel
06-11-2004, 02:11 AM
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:


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.

nolachrymose
06-11-2004, 02:38 AM
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! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum