Código PHP:
<?php
if(isset($_GET['xml'])){
header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="utf-8"?>
<personas>
<persona code="001">
<nombre>Jessica</nombre>
<apellido>Monge</apellido>
<edad>22</edad>
<sexo>F</sexo>
</persona>
</personas>
';
exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>test</title>
<script>
function importXML()
{
if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = function(){alert('valor del atributo: '+xmlDoc.documentElement.getElementsByTagName('persona').item(0).getAttribute('code'))};
}
else if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) alert('valor del atributo: '+xmlDoc.documentElement.getElementsByTagName('persona').item(0).getAttribute('code'))
};
}
else
{
alert('Your browser can\'t handle this script');
return;
}
xmlDoc.load('?xml')
}
</script>
</head>
<body>
<a href="javascript:importXML()">traer</a>
</body>
</html>