<html><head><title></title></head><body>
<script>
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false" ;
xmlDoc.load("D:/automationtest/book.xml");
txt=xmlDoc.nodeName;
document.write(txt);</script></body></html>
输出的是#document,不应该是根目录bookstore吗?我想问的就是直接加载本地电脑硬盘上的xml文件这样写对吗?
附books.xml文件
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Copyright w3school.com.cn -->
<!-- W3School.com.cn bookstore example -->
<bookstore>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
</bookstore>
<script>
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false" ;
xmlDoc.load("D:/automationtest/book.xml");
txt=xmlDoc.nodeName;
document.write(txt);</script></body></html>
输出的是#document,不应该是根目录bookstore吗?我想问的就是直接加载本地电脑硬盘上的xml文件这样写对吗?
附books.xml文件
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Copyright w3school.com.cn -->
<!-- W3School.com.cn bookstore example -->
<bookstore>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
</bookstore>
解决方案 »
- IE6,一个DIV里面绑定了事件,拖动滚动条的时候,特别卡
- |zyciis| 如何解决Jquery.Ajax 同步取得数据时 在IE中IE假死的问题,谢谢 有详细代码
- 梅花雪大哥 :关于mztreeview的编辑功能(add,remove,edit)
- Iframe 在嵌入网页的时候,能否只嵌入一部分.
- 在线编辑DOC excel文件的保存,编辑?
- 奇怪的按钮报错问题,说没有权限!没有头绪!
- 怎样能知道自己的文件被别人修改了?
- 请问怎样可以根据一个用javascript定义的二维数组动态地在网页上显示表格?
- 关于javascript的join、split方法
- jqgrid如何通过select修改行内其他单元格的数据
- 用JavaScript怎么改变输入框的属性
- 这两个正则表达式表示什么?
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("get", "cd_catalog.xml", false);//你放等xml文件路径,至于本地路径时候可行,我不知道,没试过
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
document.write (xmlDoc);
我想知道的就是这种load方法,像我这样直接的指定路径是否可以读取到xml文档内容,因为网上都找不到load方法的使用说明。
所有的例子都是类似这样的load(filename);//加载xml文档, 例子就是load(“xxx.xml”)
loadXML();//解析xml字符串
例子就是load(“xxx.xml”);
这个文档xxx.xml要么在同一个html里,要么是从别人的网址传入的,
不知道我直接写成本地电脑上的路径有什么不对呢?或者能不能这么写呢?
http://www.weste.net/book/actionscript2/index_web/12_ASD793.html
写的很清楚,出入的必须是
用法
my_xml.load(url)url 要加载的 XML 文档所位于的 URL。如果发布此调用的 SWF 文件运行在 Web 浏览器上,则 url 必须与 SWF 文件位于同一个域中;有关详细信息,请参见下面的“说明”。看来是不能写入绝对路径了该死的百度,啥都没有
xmlDoc.async = "false" ;
xmlDoc.load("books.xml");
var txt=xmlDoc.getElementsByTagName("book");
document.write ("<table>")
for (var i=0;i<txt.length;i++)
{
document .write ("<tr><td>'"+txt[i].childNodes[0].text+"'</td></tr>");
}
document.write ("</table>")
现在我这个方法是可行的,不过我用的不是本地目录,而是把books.xml放在同级目录下,至于本地的你自己试试看
js会默认为客户端的D盘路径
你把客户端放在D盘试试看看,不过我建议你用ajax方法,返回xmlDoc
<head>
<title>文档操作测试</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
//获取xml解析器 --w3school
function getXMLDoc(){
try{ //Internet Explorer
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
return xmlDoc;
}
catch(e){
try{ //Firefox, Mozilla, Opera, etc.
xmlDoc = document.implementation.createDocument("","",null);
return xmlDoc;
}
catch(e) {alert(e.message)}
}
}
function readPeoplesXML(fileName){
file = fileName;
try{
xmlDoc = getXMLDoc();
xmlDoc.async = false;
xmlDoc.load(fileName);
var root = xmlDoc.getElementsByTagName("peoples")[0];
var table = document.getElementById("table");
var peopleList = root.getElementsByTagName("people");
for(i = 0;i < peopleList.length;i ++){
var people = peopleList[i];
var row = table.insertRow();
var name = row.insertCell();
var age = row.insertCell();
name.setAttribute("align","center");
name.innerHTML = "<input type='text' size='5' id='name"+i
+ "' value='"+people.getElementsByTagName("name")[0].childNodes[0].nodeValue
+ "' onchange='save(this)'/>";
age.setAttribute("align","center");
age.innerHTML = "<input type='text' size='5' id='age"+i
+ "' value='"+people.getElementsByTagName("age")[0].childNodes[0].nodeValue
+ "' onchange='save(this)'/>";
people.getElementsByTagName("age")[0].childNodes[0].nodeValue = "22";
}
alert(fileName);
}
catch(e) {alert(e.message)}
}
var file;
function save(comt){
alert(comt.id);
}
</script>
</head>
<body>
<table id="table">
<tr>
<td>文件路径:</td>
<td><input type="file" onchange="readPeoplesXML(this.value);" size="10"></td>
</tr>
<tr><td colspan="2"> </td></tr>
<tr>
<th>姓名</th>
<th>年龄</th>
</tr>
</table>
</body>
</html><?xml version="1.0" encoding="UTF-8"?>
<peoples>
<people>
<name>赵浮云</name>
<age>12</age>
</people>
<people>
<name>钱浮云</name>
<age>12</age>
</people>
<people>
<name>孙浮云</name>
<age>12</age>
</people>
<people>
<name>李浮云</name>
<age>12</age>
</people>
</peoples>
document.write(txt);
你输出的是什么?我的是undefined。