xpath IE 7

1、

https://msdn.microsoft.com/zh-cn/library/ms756048(v=vs.85).aspx

XML内容(example.xml):

 1 <?xml version="1.0"?>
 2 <root>
 3     <branch>branch</branch>
 4     <a:root xmlns:a="http://myserver.com">
 5         <a:branch>a-branch</a:branch>
 6         <b:branch xmlns:b="http://yourserver.com">
 7             b-branch
 8         </b:branch>
 9     </a:root>
10 </root>

JS 代码:

 1     window.onload = function()
 2     {
 3     //*
 4         var dom = new ActiveXObject("MSXML2.DOMDocument.6.0");
 5         dom.async= false;
 6         dom.validateOnParse = false;
 7         //dom.load("example.xml"); // 加载 xml文件
 8         dom.loadXML(g_strXML);  // 加载 xml字符串
 9         if (dom.parseError.errorCode!=0) 
10         {
11             alert("can't load dom" + dom.parseError.reason);
12             exit;
13         }
14     //*/
15         console.log("dom : '"+dom+"' , "+typeof dom);
16     /*    for (var z in dom) // ZC: 这个dom不能使用这个方式取得它的属性/方法,会报错:"对象不支持此操作"
17         {
18             console.log(z);
19         }
20     //*/
21 //*    // *** *** ***
22         ns = "xmlns:na='http://myserver.com' xmlns:nb='http://yourserver.com'";
23 
24         console.log("ns:(before setProperty())
  "+dom.getProperty("SelectionNamespaces")); // DOMParser不能使用这种方式来检索带命名空间的节点,∵它只有一个方法 parseFromString,没有getProperty/setProperty方法
25         console.log("");
26 
27         dom.setProperty("SelectionNamespaces", ns);
28         console.log("ns:(after setProperty())
  "+dom.getProperty("SelectionNamespaces"));
29         console.log("");
30 
31         node = dom.selectSingleNode("//root");
32         console.log("root: 
"+node.xml);
33         //alert("root: 
"+node.xml);
34         console.log("");
35 
36         node = dom.selectSingleNode("//na:root");
37         console.log("a:root: 
"+node.xml);
38         console.log("");
39 
40         node = dom.selectSingleNode("//branch");
41         console.log("branch: 
"+node.xml);
42         console.log("");
43 
44         node = dom.selectSingleNode("//na:branch");
45         console.log("a:branch: 
"+node.xml);
46         console.log("");
47 
48         node = dom.selectSingleNode("//nb:branch");
49         console.log("b:branch: 
"+node.xml);
50         //alert("b:branch: 
"+node.xml);
51 //*/
52     };

输出:

“console.log("dom : '"+dom+"' , "+typeof dom);”的输出为:

日志: dom : '' , object

其他输出为(alert的输出,是和下面的值一样的。但是,IE7的console打印出来的值,每次console.log的信息最后都会烧掉一些...):

 1 ns:(before setProperty())
 2 ns:(after setProperty())
 3 xmlns:na='http://myserver.com' xmlns:nb='http://yourserver.com'
 4 
 5 root:
 6 <root>
 7 <branch>branch</branch>
 8 <a:root xmlns:a="http://myserver.com">
 9 <a:branch>a-branch</a:branch>
10 <b:branch xmlns:b="http://yourserver.com">
11 b-branch
12 </b:branch>
13 </a:root>
14 </root>
15 
16 a:root:
17 <a:root xmlns:a="http://myserver.com">
18 <a:branch>a-branch</a:branch>
19 <b:branch xmlns:b="http://yourserver.com">
20 b-branch
21 </b:branch>
22 </a:root>
23 
24 branch:
25 <branch>branch</branch>
26 
27 a:branch:
28 <a:branch xmlns:a="http://myserver.com">a-branch</a:branch>
29 
30 b:branch:
31 <b:branch xmlns:b="http://yourserver.com">
32 b-branch
33 </b:branch>

2、

http://www.w3school.com.cn/xmldom/dom_errors.asp

parseError 对象的属性

属性描述
errorCode 返回一个长整型错误码。
reason 返回包含错误原因的字符串。
line 返回表示错误行号的长整型。
linepos 返回表示错误的行位置的长整型。
srcText 返回包含引起错误的行的字符串。
url 返回指向被加载文档的 URL。
filepos 返回错误的一个长整型文件位置。

ZC-1:

(1)、上面的方式,xml 的 dom 都是通过"new ActiveXObject("MSXML2.DOMDocument.6.0");"得到的。

(2)、SVG内嵌html5的方式(https://msdn.microsoft.com/zh-cn/library/gg589526(v=vs.85).aspx) 的情况下,例如 如下代码:

这种情况下,如何得到 xml的dom?得不到xml的dom也就不能用 selectSingleNode(...)和selectNodes(...) ...

(3)、我暂时还不知道怎么处理这种方式...暂时是使用的 js原生函数 querySelector(css选择器) 和 querySelectorAll(css选择器) 来完成相关选择节点的工作的。

C

原文地址:https://www.cnblogs.com/codeskilla/p/5002964.html