求在网页上用javascript读写XML的最简单的例子 5
要直接能运行的,写入后XML文件能直接看出变化的!比如改变一个元素的值,增加一个子节点等,谢谢!一旦采用答案必追加30~50分最好是给一个HTML文件的代码和一个XML文...
要直接能运行的,写入后XML文件能直接看出变化的!比如改变一个元素的值,增加一个子节点等,谢谢!一旦采用答案必追加30~50分
最好是给一个HTML文件的代码和一个XML文件,在IE里打开网页能直接读写XML文件,不知道可不可以实现这种功能 展开
最好是给一个HTML文件的代码和一个XML文件,在IE里打开网页能直接读写XML文件,不知道可不可以实现这种功能 展开
展开全部
//把string转成xml对象
function loadXML(flag, xml) {
var xmlDoc;
if (window.ActiveXObject) {
var aVersions = ["MSXML2.DOMDocument.6.0", "MSXML2.DOMDocument.5.0", "MSXML2.DOMDocument.4.0", "MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument", "Microsoft.XmlDom"];
for (var i = 0; i < aVersions.length; i++) {
try {
xmlDoc = new ActiveXObject(aVersions[i]);
break;
} catch (oError) {
}
}
if (xmlDoc != null) {
xmlDoc.async = false;
if (flag == true) {
xmlDoc.load(xml);
} else {
xmlDoc.loadXML(xml);
}
return xmlDoc.documentElement;
}
} else {
if (document.implementation && document.implementation.createDocument) {
xmlDoc = document.implementation.createDocument("", "", null);
if (xmlDoc != null) {
if (flag == true) {
xmlDoc.async = false;
xmlDoc.load(xml);
} else {
var oParser = new DOMParser();
xmlDoc = oParser.parseFromString(xml, "text/xml");
}
return xmlDoc.documentElement;
}
}
}
return null;
}
//下面这个例子是打开一个页面上传文件,返回的是一个xml,类似<File><Type>jpg</Type><Path>...</Path>...</File> 这样的string
$("#" + attachBtnClientID).click(function () {
var a = window.showModalDialog("../FileUpload.aspx", window, "center=1, resize=1");
if (a == null || a == "undefined") {
return;
}
var xml = loadXML(false, a);
if (xml == null) {
return;
}
var index = uploadCount++;
var idNode = loadXML(false, "<ID>" + index + "</ID>");
xml.appendChild(idNode);
uploadFileXml.appendChild(xml);
var path = xml.selectNodes("SavedFilename")[0].text;
var filename = xml.selectNodes("Filename")[0].text;
var filetype = xml.selectNodes("Type")[0].text;
var html = "<td id='uploadedfile" + index + "' style='text-align:left'>";
html += "<div><div><img src='../image/" + filetype + ".jpg' /></div>";
html += "<div><a id='uploadFileLink' href='../UploadFile/" + path + "' target='_blank'>" + filename + "</a></div>";
html += "<div><a href='#' id='deleteuploadfile" + index + "'>Remove</a></div></div>";
html += "</td>";
$(html).appendTo($("#" + uploadedFileRowClientId));
$("#deleteuploadfile" + index).click(function () {
DeleteUploadedFile("uploadedfile" + index);
});
$("#" + uploadedFilesClientId).val(uploadFileXml.xml);
});
//下面是删除一个已经上传的文件,包括物理删除和在页面数据中删除
var uploadFileXml = loadXML(false, "<Files></Files>");
var DeleteUploadedFile = function (id) {
var divId = "#" + id;
$(divId).hide();
var files = uploadFileXml.selectNodes("File");
for (var i = 0; i < files.length; i++) {
var idNode = files[i].selectSingleNode("ID");
var testId = "uploadedfile" + idNode.text;
if (testId == id) {
$.ajax({
url: "../Ajax.aspx",
type: "POST",
data: "action=deletefileonly&filename=" + files[i].selectSingleNode("SavedFilename").text,
dataType: "text",
error: function (xml) { alert("Error in Ajax Call" + xml); },
success: function (data) {
}
});
uploadFileXml.removeChild(files[i]);
}
}
function loadXML(flag, xml) {
var xmlDoc;
if (window.ActiveXObject) {
var aVersions = ["MSXML2.DOMDocument.6.0", "MSXML2.DOMDocument.5.0", "MSXML2.DOMDocument.4.0", "MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument", "Microsoft.XmlDom"];
for (var i = 0; i < aVersions.length; i++) {
try {
xmlDoc = new ActiveXObject(aVersions[i]);
break;
} catch (oError) {
}
}
if (xmlDoc != null) {
xmlDoc.async = false;
if (flag == true) {
xmlDoc.load(xml);
} else {
xmlDoc.loadXML(xml);
}
return xmlDoc.documentElement;
}
} else {
if (document.implementation && document.implementation.createDocument) {
xmlDoc = document.implementation.createDocument("", "", null);
if (xmlDoc != null) {
if (flag == true) {
xmlDoc.async = false;
xmlDoc.load(xml);
} else {
var oParser = new DOMParser();
xmlDoc = oParser.parseFromString(xml, "text/xml");
}
return xmlDoc.documentElement;
}
}
}
return null;
}
//下面这个例子是打开一个页面上传文件,返回的是一个xml,类似<File><Type>jpg</Type><Path>...</Path>...</File> 这样的string
$("#" + attachBtnClientID).click(function () {
var a = window.showModalDialog("../FileUpload.aspx", window, "center=1, resize=1");
if (a == null || a == "undefined") {
return;
}
var xml = loadXML(false, a);
if (xml == null) {
return;
}
var index = uploadCount++;
var idNode = loadXML(false, "<ID>" + index + "</ID>");
xml.appendChild(idNode);
uploadFileXml.appendChild(xml);
var path = xml.selectNodes("SavedFilename")[0].text;
var filename = xml.selectNodes("Filename")[0].text;
var filetype = xml.selectNodes("Type")[0].text;
var html = "<td id='uploadedfile" + index + "' style='text-align:left'>";
html += "<div><div><img src='../image/" + filetype + ".jpg' /></div>";
html += "<div><a id='uploadFileLink' href='../UploadFile/" + path + "' target='_blank'>" + filename + "</a></div>";
html += "<div><a href='#' id='deleteuploadfile" + index + "'>Remove</a></div></div>";
html += "</td>";
$(html).appendTo($("#" + uploadedFileRowClientId));
$("#deleteuploadfile" + index).click(function () {
DeleteUploadedFile("uploadedfile" + index);
});
$("#" + uploadedFilesClientId).val(uploadFileXml.xml);
});
//下面是删除一个已经上传的文件,包括物理删除和在页面数据中删除
var uploadFileXml = loadXML(false, "<Files></Files>");
var DeleteUploadedFile = function (id) {
var divId = "#" + id;
$(divId).hide();
var files = uploadFileXml.selectNodes("File");
for (var i = 0; i < files.length; i++) {
var idNode = files[i].selectSingleNode("ID");
var testId = "uploadedfile" + idNode.text;
if (testId == id) {
$.ajax({
url: "../Ajax.aspx",
type: "POST",
data: "action=deletefileonly&filename=" + files[i].selectSingleNode("SavedFilename").text,
dataType: "text",
error: function (xml) { alert("Error in Ajax Call" + xml); },
success: function (data) {
}
});
uploadFileXml.removeChild(files[i]);
}
}
展开全部
<!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" c />
<!-- Author:hongjuesir@gmail.com Share happily !-->
<title>DSO示例</title>
</head>
<script language="javascript">
var xmlDoc;
function loadXml2Dso()
{
xmlDoc = ClassData.XMLDocument;
xmlDoc.load("ClassData.xml");
}
function moveDso()
{
var theSet = ClassData.recordset;
theSet.moveNext();
/**//*· movePrevious(): 指向前一个数据项。
· moveFirst(): 指向第一个数据项。
· moveLast(): 指向最后一个数据项。
· EOF: 这个属性用来检测我们是否已经到达数据记录的底部。*/
}
</script>
<XML id="stuData" src="DsoData.xml"></XML>
<object id="ClassData" CLASSID="clsid:550dda30-0541-11d2-9ca9-0060b0ec3d39"
width="0" height="0"></object>
<body>
<table width="80%" datasrc="#stuData" border="1" cellspacing="0" cellpadding="0">
<caption>
学生数据
</caption>
<thead>
<tr>
<th scope="col">姓名</th>
<th scope="col">年龄</th>
<th scope="col">班级</th>
</tr>
</thead>
<tbody>
<tr>
<td><span datafld="name"></span></td>
<td><span datafld="age"></span></td>
<td><span datafld="class"></span></td>
</tr>
</tbody>
</table>
<p>
<label>获取班级xml到Dso并绑定到table
<input type="submit" name="Submit" value="获取" />
</label>
</p>
<table datasrc="#ClassData" border="1">
<tr>
<td><input type="text" datafld="id" /></td>
<td><span datafld="name"></span></td>
</tr>
</table>
<p>
<label>移动DSO的RecordSet
<input type="submit" name="Submit" value="移动" />
</label>
</p>
<div>
<span datasrc="#ClassData" datafld="id" style="margin-right:20px; background-color:#0099FF"></span>
<span datasrc="#ClassData" datafld="name" style="background-color:#99CC00"></span>
</div>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml";>
<head>
<meta http-equiv="Content-Type" c />
<!-- Author:hongjuesir@gmail.com Share happily !-->
<title>DSO示例</title>
</head>
<script language="javascript">
var xmlDoc;
function loadXml2Dso()
{
xmlDoc = ClassData.XMLDocument;
xmlDoc.load("ClassData.xml");
}
function moveDso()
{
var theSet = ClassData.recordset;
theSet.moveNext();
/**//*· movePrevious(): 指向前一个数据项。
· moveFirst(): 指向第一个数据项。
· moveLast(): 指向最后一个数据项。
· EOF: 这个属性用来检测我们是否已经到达数据记录的底部。*/
}
</script>
<XML id="stuData" src="DsoData.xml"></XML>
<object id="ClassData" CLASSID="clsid:550dda30-0541-11d2-9ca9-0060b0ec3d39"
width="0" height="0"></object>
<body>
<table width="80%" datasrc="#stuData" border="1" cellspacing="0" cellpadding="0">
<caption>
学生数据
</caption>
<thead>
<tr>
<th scope="col">姓名</th>
<th scope="col">年龄</th>
<th scope="col">班级</th>
</tr>
</thead>
<tbody>
<tr>
<td><span datafld="name"></span></td>
<td><span datafld="age"></span></td>
<td><span datafld="class"></span></td>
</tr>
</tbody>
</table>
<p>
<label>获取班级xml到Dso并绑定到table
<input type="submit" name="Submit" value="获取" />
</label>
</p>
<table datasrc="#ClassData" border="1">
<tr>
<td><input type="text" datafld="id" /></td>
<td><span datafld="name"></span></td>
</tr>
</table>
<p>
<label>移动DSO的RecordSet
<input type="submit" name="Submit" value="移动" />
</label>
</p>
<div>
<span datasrc="#ClassData" datafld="id" style="margin-right:20px; background-color:#0099FF"></span>
<span datasrc="#ClassData" datafld="name" style="background-color:#99CC00"></span>
</div>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询