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
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 .
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.