Ejemplo de escritura js en xml
Al usar JS para operar XML, el cliente solo tiene permiso de lectura. Si necesita modificar y guardar el archivo XML, debe usar FSO
<. p>función go(){var thebook,root,theelem;var xmldoc=new ActiveXObject("Msxml2.DOMDocument.4.0");
xmldoc.async=false; /p>
xmldoc.load("C:\\test.xml");
root=xmldoc.documentElement;
// alert(xmldoc.xml) ;
thebook=xmldoc.createElement("libro");
// thebook.setAttribute("id" "15");
theelem=xmldoc . createElement("nombre");
theelem.text="xinshu";
thebook.appendChild(theelem);
theelem=xmldoc.createElement( " precio");
theelem.text="20";
thebook.appendChild(theelem);
theelem=xmldoc.createElement("momo" ) ;
theelem.text="¡muy bien!";
thebook.appendChild(theelem);
root.appendChild(el libro);
// alert(xmldoc.xml);thebook=root.selectSingleNode("/books/book[name='xinshu']");
thebook.setAttribute("id", 15 ");
// alert(xmldoc.xml);
thebook=root.selectSingleNode("/books/book[name='Harry Potter']"); p>
thebook.childNodes[1].text="20";
thebook.setAttribute("id","25");
// alerta (raíz .xml);
thebook.parentNode.removeChild(thebook);
alert(xmldoc.xml);
var somebook=root.selectNodes( "/ libros/libro[precio<10]");
// alert(somebook.xml);
somebook.removeAll();
alert (xmldoc .xml);
xmldoc.loadXML(xmldoc.xml);
xmldoc.save("C:\\test.xml"); //Se solicitarán permisos insuficientes. En realidad, creo que dado que DOM proporciona este método, debe tener su propósito, pero aún no he descubierto dónde se puede usar. He intentado usarlo en muchos lugares, pero aparecerán problemas de permisos. puede ser una limitación del propio JS
<p>// var fso, tf; //Se debe instalar un complemento para usar JSO
// fso = new ActiveXObject("Scripting.FileSystemObject"); / Crea un nuevo archivo
// tf = fso.CreateTextFile("c:\\testfile.xml", true);
// Completa los datos y agrega nuevas líneas
// tf.WriteLine("Pruebas 1, 2, 3.");
// Agrega 3 líneas en blanco
// tf.WriteBlankLines(3).
// p>
// Complete una línea sin saltos de línea
// tf.Write (xmldoc.xml); // Cerrar el archivo
// tf.Close();
}
Otras referencias:/Web-Guide/HTMLCSS/9335 -1.html