Ver Mensaje Individual
  #13 (permalink)  
Antiguo 10/02/2008, 12:37
Avatar de Panino5001
Panino5001
Me alejo de Omelas
 
Fecha de Ingreso: mayo-2004
Ubicación: -34.637167,-58.462984
Mensajes: 5.148
Antigüedad: 20 años
Puntos: 834
Re: Acceder a atributos de un XML con JavaScript

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>