How to parse this xml in mozilla

K

KB

Hi folks,

I'm trying to adopt microsoft-specific code to mozilla. I have an xml
file which I can't figure out how to parse using the XMLDocument
object:

<?xml version="1.0" encoding="utf-16" ?>
<locations>
<loc id="401" name="one" />
<loc id="402" name="two" />
<loc id="403" name="three" />
</locations>

here's the code:

var xmlfile = <above...>
xmldoc = document.implementation.createDocument( "", "", null );
xmldoc.addEventListener("load", onload, false);
xmldoc.load( xmlfile );
.....
.....
function onload( evt )
{
// the following code works in IE, but in NS childNodes is
empty
var locs = xmldoc.childNodes[0];
for ( var i=0; i<locs.childNodes.length; i++ )
{
elem = locs.childNodes;
if ( elem.nodeName == "loc" )
{
var id = elem.attributes[0].value;
var name = elem.attributes[1].value;
alert( 'location : ' + id + ',' + name );
}
}
}

Any help is greatly appreciated
Kevin
 
M

Martin Honnen

KB wrote:

I'm trying to adopt microsoft-specific code to mozilla. I have an xml
file which I can't figure out how to parse using the XMLDocument
object:

<?xml version="1.0" encoding="utf-16" ?>
<locations>
<loc id="401" name="one" />
<loc id="402" name="two" />
<loc id="403" name="three" />
</locations>

here's the code:

var xmlfile = <above...>

That should be a URL e.g.
var xmlfile = "file.xml";
xmldoc = document.implementation.createDocument( "", "", null );
xmldoc.addEventListener("load", onload, false);

Don't use a function named onload as that is the window.onload handler,
anything else is fine e.g.
xmldoc.addEventListener("load", xmlLoadHandler, false);
xmldoc.load( xmlfile );
....
....
function onload( evt )

function xmlLoadHandler (evt) {
{
// the following code works in IE, but in NS childNodes is
empty
var locs = xmldoc.childNodes[0];

Better
var locs = xmldoc.documentElement;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top