以下是XML文件【1.xml】内容:
<?xml version="1.0" encoding="gb2312"?>
<config>
<content>
<content_1>1.我的内容1.</content_1>
<content_2>2.我的内容2.</content_2>
</content>
</config>
以下是ASP文件内容【1.asp 用于管理1.xml】:
<%
strSourceFile = Server.MapPath("1.xml")
Set objXML = Server.CreateObject("Microsoft.XMLDOM") '创建一个XML对像
'Set objXML = Server.CreateObject("Microsoft.FreeThreadedXMLDOM")
objXML.load(strSourceFile) '把XML文件读入内存
If objXML.parseError.ErrorCode <>0 Then
objXML.loadXML "<?xml version=""1.0"" encoding=""gb2312"" ?>"
End If
set objcontent = objXML.documentElement.SelectSingleNode("content") '读取节点content
if request("tijiao")="yes" then '判断是否是提交过来的
objcontent.SelectSingleNode("content_1").text=request.form("content_1")
objcontent.SelectSingleNode("content_2").text=request.form("content_2")
objXML.save(strSourceFile) '将提交过来的表单数据写入XML
response.Write("<script>alert('保存成功!')</script>")
end if
%>
<html><head>
<TITLE>XML文件读写实例</TITLE>
<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312">
</head><body>
<table width=98% align="center" border="1" cellspacing="0" cellpadding="4" style="border-collapse: collapse">
<form name="form1" method="post" action="">
<input type="hidden" value="yes" name="tijiao"><!--这里一个隐形的input用于提交判断-->
<tr><td height=25>XML文件读写实例</td></tr>
<tr>
<td>内容1: <input name="content_1" type="text" id="content_1" value="<%=objcontent.SelectSingleNode("content_1").text%>" size="60">
</td>
<tr>
<td>内容2: <input name="content_2" type="text" id="content_2" value="<%=objcontent.SelectSingleNode("content_2").text%>" size="60">
</td>
</tr>
<tr align="center">
<td height="36">
<input type="submit" name="Submit" value="保存">
</td>
</tr></form>
</table>
</body>
</html>
被调用显示文档也使用ASP,参照input内显示的值!
HTML文件:<!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/htmlcharset=gb2312" />
<title>DOMTest2 </title>
<script language="javascript">
var request = false
try {
request = new XMLHttpRequest()
} catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP")
} catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP")
} catch (failed) {
request = false
}
}
}
if (!request)
alert("Error initializing XMLHttpRequest!")
function callServer() {
var firstName = document.getElementById("firstName").value
var lastName = document.getElementById("lastName").value
var street = document.getElementById("street").value
var city = document.getElementById("city").value
var state = document.getElementById("state").value
var zipCode = document.getElementById("zipCode").value
var xmlString = " <profile>" +
" <firstName>" + escape(firstName) + " </firstName>" +
" <lastName>" + escape(lastName) + " </lastName>" +
" <street>" + escape(street) + " </street>" +
" <city>" + escape(city) + " </city>" +
" <state>" + escape(state) + " </state>" +
" <zip-code>" + escape(zipCode) + " </zip-code>" +
" </profile>"
// Build the URL to connect to
var url = "/Ajax/scripts/saveAddress.xml"
// Open a connection to the server
request.open("POST", url, true)
// Tell the server you're sending it XML
request.setRequestHeader("Content-Type", "text/xml")
// Set up a function for the server to run when it's done
request.onreadystatechange = updatePage
// Send the request
request.send(xmlString)
}
function updatePage() {
if (request.readyState == 4) {
if (request.status == 200) {
var xmlDoc = request.responseXML
var showElements = xmlDoc.getElementsByTagName("show")
for (var x=0x <showElements.lengthx++) {
alert("Operator XML file.")
// We know that the first child of show is title, and the second is rating
var title = showElements[x].childNodes[0].value
var rating = showElements[x].childNodes[1].value
document.getElementById("title").value=title
document.getElementById("rating").value=rating
// Now do whatever you want with the show title and ratings
}
}
else if (request.status == 404)
{ alert("Request URL does not exist")}
else if (request.status == 403) {
alert("Access denied.")
}
else
alert("Error: status code is " + request.status)
}
}
</script>
</head>
<body>
<div id="firstName">aaaa </div>
<div id="lastName">bbbb </div>
<div id="street">cccc </div>
<div id="city">dddd </div>
<div id="state">eeee </div>
<div id="">ffff </div>
<div id="zipCode">12334 </div>
<input id="title" type="text"/>
<input id="rating" type="text"/>
<input type="button" value="Click me!" onclick="callServer()" />
</body>
</html>
XMML文件:
saveAddress.xml
<?xml version="1.0" encoding="gb2312"?>
<ratings>
<show>
<title>Alias </title>
<rating>6.5 </rating>
</show>
<show>
<title>Lost </title>
<rating>14.2 </rating>
</show>
<show>
<title>Six Degrees </title>
<rating>9.1 </rating>
</show>
</ratings>
1,内嵌XML数据<html>
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312">
<title>第一次亲密接触(非痞子蔡版,请不要理解为侵权)</title>
</head>
<body>
<xml id="第一次亲密接触">
<第一次亲密接触>
<对象>xml</对象>
<亲密程度>接触到XML稍微宝贵的地方---数据岛</亲密程度>
<结论>感觉不错,是个好东东</结论>
</第一次亲密接触>
</xml>
<center><h1>第一次亲密接触</h1></center>
<table border="0" datasrc="#第一次亲密接触" align="center" width="443">
<tr>
<td bgcolor="#99FF99">对象: <span datafld="对象"></span></td>
<td bgcolor="#3399CC">亲密程度:<span datafld="亲密程度"></span></td>
<td bgcolor="#CC99CC">结论: <span datafld="结论"></span></td>
</tr>
</table>
</body>
</html>
2.外部xml文件的引用
首先利用以前的知识建立一个XML文件(不是我懒,给你们一个机会锻炼^^)
涉及到以下<xml></xml>中的数据内容
<html>
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312">
<title>第一次亲密接触(非痞子蔡版,请不要理解为侵权)</title>
</head>
<body>
<xml id="第一次亲密接触" src="firstlove.xml">
<第一次亲密接触>
<对象>xml</对象>
<亲密程度>接触到XML稍微宝贵的地方数据岛</亲密程度>
<结论>感觉不错,是个好东东</结论>
</第一次亲密接触>
</xml>
<center><h1>第一次亲密接触</h1></center>
<table border="0" datasrc="#第一次亲密接触" align="center">
<tr>
<td bgcolor="#99FF99">对象: <span datafld="对象"></span></td>
<td bgcolor="#3399CC">亲密程度:<span datafld="亲密程度"></span></td>
<td bgcolor="#CC99CC">结论: <span datafld="结论"></span></td>
</tr>
</table>
</body>
</html>
我们可以通过指定XML文件位置来引用她,一般通过src="XXX.xml"引用.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)