miércoles, 26 de septiembre de 2007

Parsear un fichero XML con javascript

Para poder parsear un fichero XML desde javascript tenemos que hacer:

var xmlDoc;
function loadXML(archivoXML) {



if (window.ActiveXObject) {
//--- Creo un nuevo objeto de la librería Microsoft.XMLDOM que es la que se encarga
//--- en Internet Explorer de 'parsear' un archivo XML.
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
data = archivoXML;//"http://google.es";//
xmlHttp.open("GET", data, false);
xmlHttp.send(archivoXML_2);
xmlDoc = xmlHttp.responseXML;
mostrarDatosIe();
} else {
if (document.implementation && document.implementation.createDocument) {
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.load(archivoXML);

xmlDoc.onload = mostrarDatosFx;
} else {
alert("Su navegador no puede soportar este script");
}
}
}

function mostrarDatosFx() {
var listaParcelas = xmlDoc.getElementsByTagName("Parcela");


document.formLicencias.mapa_pdf.value = xmlDoc.getElementsByTagName("LicenciaUrbanistica")[0].getElementsByTagName("Plano").item(0).textContent;
}
function mostrarDatosIe() {

var listaParcelas = xmlDoc.getElementsByTagName("Parcela");

document.formLicencias.mapa_pdf.value = xmlDoc.getElementsByTagName("LicenciaUrbanistica")[0].getElementsByTagName("Plano").item(0).text;
}

Variables Globales con TOMCAT

Para poder tener variables de configuración en un proyecto web con TOMCAT debemos hacer:
añadir los diferentes parámetros al fichero web.xml


dir_ficherosXML
xml/


y luego en los diferentes servlets que usemos:


public void init(ServletConfig config) throws ServletException {
super.init(config);
dir_real = config.getServletContext().getRealPath("/");
dir_ficherosXML = dir_real+
config.getServletContext().getInitParameter("dir_ficherosXML");
}


Con esto ya tenemos variables como por ejemplo la dirección de tal o cual directorio sin que tengamos que cambiar el código fuente cada vez que hagamos una nueva instalación.