Parsing XML in Javascript
I have an xml file formatted as follows...
Code:
<?xml version='1.0' encoding='utf-8'?>
<DeathCert>
<Item><Title>Name</Title><Data>Joe Fritz</Data><Link>12345</Link></Item>
<Item><Title>Death Date</Title><Data>Someday</Data></Item>
<Item><Title>Father</Title><Data>Bob Fritz</Data><Link>41254</Link></Item>
<Item><Title>Informant</Title<Data>Mary Fritz</Data<Show>Y</Show></Item>
</DeathCert>
I use the following code to access the elements of the file...
Code:
xmlDoc = $.parseXML( xmlDeathCert );
$('#xml_name').val(xmlDoc.getElementsByTagName('Data')[0].childNodes[0].nodeValue);
$('#xml_death_date').val(xmlDoc.getElementsByTagName('Data')[1].childNodes[0].nodeValue);
$('#xml_fathers_name').val(xmlDoc.getElementsByTagName('Data')[2].childNodes[0].nodeValue);
This works fine! The problem that I am facing is how to access the elements of 'Link' and 'Show'. I would think that the code would be...
Code:
$('#xml_name_id').val(xmlDoc.getElementsByTagName('Link')[0].childNodes[0].nodeValue);
$('#xml_fathers_id').val(xmlDoc.getElementsByTagName('Link')[1].childNodes[0].nodeValue);
Which works for the first but not for the second. Same for the 'Show' elements.
Further, I would think (it often gets me in trouble) that the access to the tree would be as such...
Code:
var Name = xmlDoc.getElementsByTagName('Item')[0].childNodes[1].nodeValue;
Representing the '0' element in the 'Item' and the 1st childnode (or 'Data').
Any assistance understanding how to access the 'Link' and 'Show' elements of the above tree will be greatly appreciated.
TIA
jdadwilson