如何用js动态的生成一个xml文件

如何用js动态的生成一个xml文件,第1张

var str

str ='<?xml version="1.0" encoding="utf-8" ?><?xml-stylesheet type="text/xsl" href= "example.xsl"?>'

str += "<Goods><id>"

生成字符串,然后用FSO生成文件啊fso.CreateTextFile

在ie6下面试过的 你看一下

<html>

<head>

<head>

<body><script>

<!--

function flvlist($name,$t) {

var fso, tf

fso = new ActiveXObject("scripting.FileSystemObject")

// 创建新文件

tf = fso.CreateTextFile("c:\\test.xml", true)

// 填写数据,并增加换行符

tf.WriteLine(" <?xml version=\"1.0\" encoding=\"utf-8\"?>")

tf.WriteLine(" <player showDisplay=\"yes\" showPlaylist=\"yes\" autoStart=\"yes\">")

//tf.WriteLine(" <item item_url=\"../flv/\"+$name+".flv" item_title="+$t+"></item>")

tf.WriteLine(" <song path=\""+$name+"\" title=\"" + $t + "\">")

tf.WriteLine(" </player>")

// 关闭文件

tf.Close()

}

-->

</script>

<form name="form1" method="post" action="">

<p>

<input name="aa" />

<input name="bb" />

</p>

<p>

<label>

<input type="submit" name="Submit" value="提交" onclick=javascript:flvlist(document.getElementsByName("aa")[0].value,document.getElementsByName("bb")[0].value)>

</label>

</p>

</form>

</body>

<html>

用JS *** 作XML,对客户端来说只有读取的权限如果需要对XML文件进行修改保存 *** 作,就要用FSO<script type="text/javascript">

function go(){var thebook,root,theelem

var xmldoc=new ActiveXObject("Msxml2.DOMDocument.4.0")

xmldoc.async=false

xmldoc.load("C:\\test.xml")

root=xmldoc.documentElement

// alert(xmldoc.xml)

thebook=xmldoc.createElement("book")

// thebook.setAttribute("id" "15")

theelem=xmldoc.createElement("name")

theelem.text="xinshu"

thebook.appendChild(theelem)

theelem=xmldoc.createElement("price")

theelem.text="20"

thebook.appendChild(theelem)

theelem=xmldoc.createElement("momo")

theelem.text="very good!"

thebook.appendChild(theelem)

root.appendChild(thebook)

// alert(xmldoc.xml)thebook=root.selectSingleNode("/books/book[name='xinshu']")

thebook.setAttribute("id","15")

// alert(xmldoc.xml)

thebook=root.selectSingleNode("/books/book[name='哈里波特']")

thebook.childNodes[1].text="20"

thebook.setAttribute("id","25")

// alert(root.xml)

thebook.parentNode.removeChild(thebook)

alert(xmldoc.xml)

var somebook=root.selectNodes("/books/book[price<10]")

// alert(somebook.xml)

somebook.removeAll()

alert(xmldoc.xml)

xmldoc.loadXML(xmldoc.xml)

xmldoc.save("C:\\test.xml") //会提示权限不足..其实我在想 既然DOM给出了这个方法必然有它的用途,但是目前还没发现它在哪能用到 . 试过多个地方使用 都会出现权限问题 可能是JS本身的限制

// var fso, tf //使用JSO必须安装个插件

// fso = new ActiveXObject("Scripting.FileSystemObject")

// 创建新文件

// tf = fso.CreateTextFile("c:\\testfile.xml", true)

// 填写数据,并增加换行符

// tf.WriteLine("Testing 1, 2, 3.")

// 增加3个空行

// tf.WriteBlankLines(3)

// 填写一行,不带换行符

// tf.Write (xmldoc.xml)

// 关闭文件

// tf.Close()

}

</script>其它参考:http://www.68design.net/Web-Guide/HTMLCSS/9335-1.html


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11847319.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存