1

I am reading a XML file through SpineTix which is built on Javascript. I've created a custom parser for this.

This is the XML structure

<ArlandaExpress>
<info>
<updated date="2013-06-10" time="10:28:42"/>
<message priority="1">
<![CDATA[
Biljettköp ombord kostar 100:- extra, ticket on board 100 SEK extra
]]>
</message>
<message priority="2">
<![CDATA[
Biljettköp ombord kostar 100:- extra, ticket on board 100 SEK extra
]]>
</message>
<message priority="3">
<![CDATA[
Restid 20 min. 2 för 380 kr tor-sön och röda dagar t.o.m.16/6// Traveltime 20 min. 2 for 380 SEK Thu-Sun and bank holidays until 16/6
]]>
</message>
</info>
  <StockholmC>
    <next minutes="6"/>
    <upcoming datetime="2013-06-10 10:50"/>
    <upcoming datetime="2013-06-10 11:05"/>
    <upcoming datetime="2013-06-10 11:20"/>
  </StockholmC>
</ArlandaExpress>

I need to grab the minutes and datetime attributes in the StockholmC tag. How can I do this? This is my code so far which by the way works if you remove all other tags from the document but StockholmC

function custom_parser( response, records ){
   var rssDocument = parseXML( response );
   if ( rssDocument==null ) return;
   for ( var row=rssDocument.documentElement.firstElementChild; row!=null;   row=row.nextElementSibling ) {
      var r =  new Object();      
      r.next = row.getAttribute('minutes');
      r.date = row.getAttribute('datetime');
      records.push( r );
   }
}

I want to point out that the above code works in a XML file with ONLY the StockholmC tag.

Thank you

1

2 Answers 2

2

You are iterating over every child of the root, starting with <info> (rssDocument.documentElement.firstElementChild). Those elements don't have minutes or datetime attributes.

If you only want to iterate over the children of <StockholmC>, you have to get a reference to it instead of documentElement:

function custom_parser( response, records ){
   var rssDocument = parseXML( response );
   if (!rssDocument) return;
   var stockholmc = rssDocument.getElementsByTagName('StockholmC')[0];
   if (!stockholmc) return;
   for (var row=stockholmc.firstElementChild; row!=null; row = row.nextElementSibling) {
       // ...
   }
}

The DOM interface provides many methods to traverse the DOM tree. Have a look at the introduction to DOM on MDN.

3
  • TypeError: rssDocument.getElementsByTagName is not a function: at project://02110_Weather_RSS/news_ticker.svg:170 It complains at this line var stockholmc = rssDocument.getElementsByTagName('StockholmC')[0];
    – anonamas
    Commented Jun 10, 2013 at 8:50
  • I've been mixing with your answer and I cant get it to work. Now it doesn't display anything. Even tried to clone your code.
    – anonamas
    Commented Jun 10, 2013 at 9:11
  • What about rssDocument.documentElement.getElementsByTagName? Commented Jun 10, 2013 at 9:53
1

As explained by Felix, you first need to iterate over the children of <StockholmC>. However, as mentioned in your question, as you are using JavaScript inside a SpinetX HMP, you can only rely on a subset of the DOM level 3 (see SVG Micro DOM) and thus, cannot use the getElementsByTagName().

Here is an example of iterating the DOM until you find the correct element.

function custom_parser( response, records ){
  var rssDocument = parseXML( response );
  if ( rssDocument==null ) return;
  var elem = rssDocument.documentElement.firstElementChild;
  while ( elem!=null ) {
    if ( elem.localName=="StockholmC" ){
      elem = elem.firstElementChild;
    } else {
      if ( elem.localName=="next" ) {
        var r =  new Object();     
        r.next = elem.getAttribute('minutes');
        records.push( r );
      } else if ( elem.localName=="upcoming" ) {
        var r =  new Object();     
        r.datetime = elem.getAttribute('datetime');
        records.push( r );
      }
      elem = elem.nextElementSibling;
    }
  }
}

Note that the sample code puts the next and datetime attribute in two different rows of your array.

1
  • This is funny, it only displays next one time and it does only display one upcoming. Current display: Next departure in 7 minutes. Following 09:30. What I need: Next departure in 7 minutes. Following 09:30 10:30 11:30. Currently it seem to push out the data one at a time and not collecting them before display.
    – anonamas
    Commented Jun 24, 2013 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.