For any XML node with attributes it should be standard practice to extract all attributes and only store the ones you need. By specifically referencing each attribute you are essentially hard-coding the attribute names in your code. The beauty of XML is that I don't care about "extra" attributes. Pull them all off (using a for..in approach) and only keep the ones I am interested in. That way if the source XML changes I don't have to modify code to account for new attributes (unless I want to).
Code: Select all
JavaScript example:
function ParseAttr(inXMLNodeElement) {
var parseResults = new Object; var attrName, attrValue;
for (var childCntr=0; childCntr < inXMLNodeElement.attributes.length; childCntr++) {
attrName = inXMLNodeElement.attributes[childCntr].nodeName.toUpperCase();
attrValue = inXMLNodeElement.attributes[childCntr].nodeValue;
parseResults[attrName] = attrValue;
}
return parseResults;
}
example:
Code: Select all
var x = ParseAttr(PITCHLevel.childNodes[1]); // x will contain all of the attributes of any element node you pass
if (PITCHLevel.childNodes[1].Name == 'EVENT') {
if (x.FIELDERS) {// now we know we will have some fielder nodes}
if (x.PLAYER) {// now we know we have a runner/hitter event}
if (x.TYPE == 'LOC') {// now we have a hit chart location}
}