How to parse this xml in mozilla

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • KB

    How to parse this xml in mozilla

    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.implem entation.create Document( "", "", null );
    xmldoc.addEvent Listener("load" , onload, false);
    xmldoc.load( xmlfile );
    .....
    .....
    function onload( evt )
    {
    // the following code works in IE, but in NS childNodes is
    empty
    var locs = xmldoc.childNod es[0];
    for ( var i=0; i<locs.childNod es.length; i++ )
    {
    elem = locs.childNodes[i];
    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
  • mscir

    #2
    Re: How to parse this xml in mozilla

    KB wrote:
    [color=blue]
    > 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:[/color]
    <snip>

    You might give some google hits a look:




    MIke

    Comment

    • Martin Honnen

      #3
      Re: How to parse this xml in mozilla



      KB wrote:

      [color=blue]
      > 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...>[/color]

      That should be a URL e.g.
      var xmlfile = "file.xml";
      [color=blue]
      > xmldoc = document.implem entation.create Document( "", "", null );
      > xmldoc.addEvent Listener("load" , onload, false);[/color]

      Don't use a function named onload as that is the window.onload handler,
      anything else is fine e.g.
      xmldoc.addEvent Listener("load" , xmlLoadHandler, false);
      [color=blue]
      > xmldoc.load( xmlfile );
      > ....
      > ....
      > function onload( evt )[/color]

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

      Better
      var locs = xmldoc.document Element;


      --

      Martin Honnen

      Comment

      Working...