在xml如何添加数据?

hling 2003-10-15 04:49:53
我有一个XML文件是这样的:


<?xml version="1.0" encoding="gb2312"?>
<?xml-styleshee type='text/xsl' href='corporationsxl.xsl'?>
<root>
<name>
<co>a</co>
<job>a1</job>
</name>
<name>
<co>b</co>
<job>b1</job>
</name>
</root>

我用添加数据成为:
<?xml version="1.0" encoding="gb2312"?>
<?xml-styleshee type='text/xsl' href='corporationsxl.xsl'?>
<root>
<name>
<co>a</co>
<job>a1</job>
</name>
<name>
<co>b</co>
<job>b1</job>
</name>
<name>
<co>c</co>
<job>c1</job>
</name>
<name>
<co>d</co>
<job>d1</job>
</name>
</root>

这个程序用XMLDOM怎么写?谢谢


...全文
134 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2003-10-15
  • 打赏
  • 举报
回复
把xml保存为a.xml

<script>
var x = new ActiveXObject("Microsoft.XMLDOM")
x.async=false
x.load("a.xml")
alert(x.xml)
n = x.createElement("name")
n1 = x.createElement("co")
n1.text="c"
n.appendChild(n1)
n1 = x.createElement("job")
n1.text="c1"
n.appendChild(n1)
x.documentElement.appendChild(n)

n = x.createElement("name")
n1 = x.createElement("co")
n1.text="d"
n.appendChild(n1)
n1 = x.createElement("job")
n1.text="d1"
n.appendChild(n1)
x.documentElement.appendChild(n)
alert(x.documentElement.xml)
</script>
purexu 2003-10-15
  • 打赏
  • 举报
回复
Set oXmlNode = oXmlObj.selectSingleNode("/root")
Set oElement = oXmlObj.createElement("name")
Set oNode = oXmlObj.createElement("co")
Set oText = oXmlObj.createTextNode("d")
Set oNode1 = oXmlObj.createElement("job")
Set oText1 = oXmlObj.createTextNode("cl")
oNode.appendChild oText
oNode1.appendChild oText1
oElement.appendChild oNode
oElement.appendChild oNode1
oXmlNode.appendChild oElement
Set oElement = Nothing
Set oXmlNode = Nothing
wolf004 2003-10-15
  • 打赏
  • 举报
回复
sourcefile=Server.MapPath("7book3.xml")
这句的文件名改一下!
sourcefile=Server.MapPath("7book2-1.xml")
wolf004 2003-10-15
  • 打赏
  • 举报
回复
例子:
--------------------7book2-2.asp-----------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>

<body>

<%dim printdata,url,title,author,press,price,impression,resume1
printdata=request("printdata")
url=request("url")
title=request("title")
author=request("author")
press=request("press")
price=request("price")
impression=request("impression")
resume1=request("resume")
response.write "是否加入了新的节点?" & cbool(addpersondata(printdata,url,title,author,press,price,impression,resume1))&"<br><p>"
%>
<%dim objxml,root,i
set objxml=server.CreateObject("microsoft.freethreadedxmldom")
objxml.load(Server.MapPath("7book3.xml"))
set root=objxml.documentelement
response.Write "<hr size=1>"
response.write "<p>新增一个节点后的7book3.xml内容如下:<p>"
for i=0 to root.childnodes.length-1
response.write root.childnodes.item(i).text
response.write "<p>"
next
set objxml=nothing%>
<%function addpersondata(printdata, url, title, author, press, price, impression, resume1)
dim xmlcode,sourcefile,xobj1,xobj2
dim rootnode ,newnode,currentnode
addpersondata=0
xmlcode=xmlcode&"<book printdata="""&printdata &" url="&url
xmlcode=xmlcode& """><title>"&title&"</title>"
xmlcode=xmlcode&"<author>"&author&"</author>"
xmlcode=xmlcode&"<press>"&press&"</press>"
xmlcode=xmlcode&"<price>"&price&"</price>"
xmlcode=xmlcode&"<impression>"&impression&"</impression>"
xmlcode=xmlcode&"<resume>"&resume1&"</resume></book>"
sourcefile=Server.MapPath("7book3.xml")
set xobj1=server.CreateObject("microsoft.xmldom")
set xobj2=server.CreateObject("microsoft.xmldom")
xobj1.load(sourcefile)
set rootnode=xobj1.documentelement
if not isobject(rootnode) then
exit function
end if
xobj2.loadxml(xmlcode)
set newnode=xobj2.documentelement
set currentnode=rootnode.appendchild(newnode)
xobj1.save(sourcefile)
set xobj2=nothing
set xobj1=nothing
addpersondata=1
end function
%>
</body>
</html>

---------------------------7book2-1.xml------------------------
<?xml version="1.0" encoding="gb2312" ?>
<?xml-stylesheet type="text/xsl" href="7book2.xsl"?>
<bookInformation xmlns:dt="urn:schemas-microsoft-com:datatypes">
<book Printdate="2001">
<title>电子商务基础</title>
<author>唐春林</author>
<press>科学出版社</press>
<price dt:dt="number">24.80</price>
<impression dt:dt="number">6000</impression>
<resume >本书介绍电子商务的基础知识及涉及的主要技术,主要偏重于对电子商务的感性认识和参与电子商务的实践(配有相关的上机实践题),并给出了一些电子商务的成功案例。本书既可作为大、中专院校相关课程的参考教材,也可作为政府部门。各机关、企事业单位的“电子商务”培训教材。
</resume>
</book>

<book Printdate="2001">
<title> Flash 5 白皮书</title>
<author>张仁川</author>
<press>科学出版社</press>
<price dt:dt="number">61.00</price>
<impression dt:dt="number">8000</impression>
<resume>本书以精美的实例,向读者介绍Flash 5的各种功能和操作技巧。图文对照、版式活泼是本书的重要特点,作者精心制作的实例定能引导您进入Flash的动画世界。
</resume>
</book>

<book Printdate="2000">
<title>网上筑巢</title>
<author>浮云</author>
<press>科学出版社</press>
<price dt:dt="number">12.50</price>
<impression dt:dt="number">9000</impression>
<resume>本书通过对于流行的网页制作软件FrontPage 2000的介绍,详细讲述了制作个人主页及个人网站的全过程。本书依照循序渐进的原则,从基础入手,逐步学习如何从无到有地做一个精美的网页,以及如何建立、管理和发布Web站点。
</resume>
</book>

<book Printdate="2000">
<title> Photoshop 高级应用技巧</title>
<author>王炜</author>
<press>科学出版社</press>
<price dt:dt="number">26.00</price>
<impression dt:dt="number">10000</impression>
<resume>本书以生动、翔实的实例介绍了应用Photoshop 5.0进行高级图象处理的方法,使读者从实例人手,轻松掌握Photoshop5.0的精髓和应用技巧。本书共分十一章,第一章介绍了进行图象处理的基本理论;第二、三、四、五章从典型实例入手,以实际创作的方法向读者展现了Photoshop的关键技术和使用技巧;第六章、第七章利用滤镜对图象进行特殊效果处理;第八章。第十一章综合运用本书的高级技术,创造出几个典型实例,引导读者进入栩栩如生的图象世界。
</resume>
</book>

<book Printdate="2000">
<title> OUTLOOK 2000 实战入门</title>
<author>马军</author>
<press>科学出版社</press>
<price dt:dt="number">29.00</price>
<impression dt:dt="number">7000</impression>
<resume>本书从Outlook 2000中文版使用者的角度出发,深入浅出地讲述了如何掌握和使用Outlook 2000中文版的功能和应用。全书内容丰富,解释详尽。书中列举了大量实例、步骤和插图,可供读者对照着学习操作。本书是一本实用性很强的 Outlook 2000的学习指南。
</resume>
</book>
</bookInformation>


8,906

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧