JS XML


Javascript DOM can be used to parse XML. Following is a XML string:

var str='<ResultSet date="2012-02-26 07:43:10">' +
'<Query>TTRE</Query>' +
'<Hits>1</Hits>' +
'<Result id="1">' +
'<analysis_id>2a891a941fe3</analysis_id>' +
'<state>live</state>' +
'<last_modified>2011-08-26T07:00:00Z</last_modified>' +
'<upload_date></upload_date>' +
'<center_name>XTRCD</center_name>' +
'<study>phs000178</study>' +
'<aliquot_id>4a8c8a7df3a5</aliquot_id>' +
'<files>' +
'<file>' +
'<filename>test.txt</filename>' +
'<filesize>59264187</filesize>' +
'</file>' +
'</files>' +
'' +
'</Result>' +
'</ResultSet>';


JS code to parse the XML:

<script language=javascript>
var xl;
if (typeof(window.DOMParser != "undefined"))
{
	var p = new DOMParser();
	var xl = p.parseFromString(str,"text/xml");
}
else if (typeof(window.ActiveXObject != "undefined"))
{
	var xl=new ActiveXObject("Microsoft.XMLDOM");
	xl.async=false;
	xl.loadXML(str);
}
else document.write("no XML parser!");
var filename=xl.getElementsByTagName("filename")[0].childNodes[0].nodeValue;
document.write(filename); //test.txt
</script>