在IE中使用命名空间解析XML。 [英] Parsing XML with namespaces in IE.

查看:63
本文介绍了在IE中使用命名空间解析XML。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个新闻组和谷歌的帮助下,我得到了这个代码

完全在Firefox中运行并且可以在IE中提醒XML,但是因为IE

并不意味着DOMgetElementsByTagNameNS()函数我

无法从Cube命名空间中读取单独的费率。


是否有包装或其他一些相对简单的获取方法

IE在Firefox中做什么是直截了当的?


这是代码。任何帮助都感激不尽。


var doc

函数load(){

if(document.implementation&&& br />
document.implementation.createDocument){

doc = document.implementation.createDocument("","",null);

doc.load(''CEBrates.xml'');

doc.onload = createTable;

}

else if(window。 ActiveXObject){

var doc1 = new ActiveXObject(" Microsoft.XMLDOM");

函数loadXML(xmlFile){

doc1.async =" false";

doc1.onreadystatechange = verify;

doc1.load(xmlFile);

doc = doc1.documentElement; < br $>
}

loadXML(''CEBrates.xml'');

alert(doc.xml)

}

else {

alert(''你的浏览器不能处理这个脚本');

return;

}

}

函数verify(){

if(doc1.readyState!= 4){

返回false;

}

}

函数createTable(){

var cubes = doc.getElementsByTagNameNS(' 'http://www.ecb.int/

词汇/ 2002-08-01 / eurofxref'',''Cube'');

var dateRate = cubes [1] .getAttribute(''time'');

var dateRateSplit = dateRate.split('' - '');

for(var i = 2;我< cubes.length; i ++){

var currency = cubes [i] .getAttribute(''currency'');

var rate = cubes [i] .getAttribute(''rate' ');

rateObject [货币] =费率;

}

document.getElementById(''boldStuff'')。innerHTML = dateRateSplit [ 2]

+"。" + dateRateSplit [1] +"。" + dateRateSplit [0];

document.getElementById(" currency")。value =" GBP";

getRates();

};

var rateObject = {};

函数getRates(){

var curr = document.getElementById(" currency") .value;

var currRate = rateObject [curr];

var currStatement =" 1 EUR =" + currRate +" " + curr;

document.getElementById(" rate")。value = currStatemen t;

var currRev = 1 / currRate;

currRevFix = currRev.toFixed(5);

var currRevStatement =" 1" + curr +" =" + currRevFix +" EUR" ;;

document.getElementById(" rateRev")。value = currRevSt atement;

};

解决方案

11月2日下午3:10 *,Steve< stephen.jo ... @ googlemail.comwrote:


借助帮助这个新闻组和谷歌我有这个代码

完全在Firefox中工作,可以在IE中提醒XML,但因为IE

不会暗示DOMgetElementsByTagNameNS() "函数I

无法读取Cube命名空间中的各个速率。



目前,MS XML DOM对象不支持

getElementsByTagNameNS方法,但它肯定能够读取

来自文档中元素的数据。请参阅getElementsByTagName,

selectNodes和selectSingleNode。

http://msdn.microsoft.com/en-us/libr...28(VS.85).aspx


>

是否有一个包装器或其他一些相对简单的方法来获取

IE在Firefox中做什么直截了当?


这是代码。任何帮助都感激不尽。


var doc

函数load(){

* * if(document.implementation&& ;

document.implementation.createDocument){



始终使用typeof测试宿主方法(例如应该是object,

" function"或unknown。)通过布尔类型测试宿主方法

转换已知会导致IE中的异常。


* * * * * * * * doc = document.implementation.createDocument("","",null);

* * * * * * * * doc.load(''CEBrates.xml'');

* * * * * * * * doc.onload = createTable;

* * * *}

* * * * if if(window.ActiveXObject){



我会测试的不仅仅是真实性这里(应该是一个函数。)

你将需要一个try-catch子句来处理不允许使用

ActiveX对象的情况,XML DOM对象是发生故障



实例化可能出错的其他任何事情。


* * * * * * * * var doc1 = new ActiveXObject(" Microsoft..XMLDOM");

* * * function loadXML(xmlFile){

* * * * * doc1.async = QUOT;假英寸;



为什么要为字符串设置布尔属性?你很幸运

对象没有抛出异常。无法知道未来的Windows

更新是否会破坏这一点。


* * * * * doc1.onreadystatechange = verify;

* * * * * doc1.load(xmlFile);

* * * * * doc = doc1.documentElement;

* * *}

* * * loadXML(''CEBrates.xml'');

* * * alert(doc.xml)



使用window.alert。


* * *}

* * * else {

* * * * * * * * alert(''您的浏览器无法处理此脚本'');



永远不要这样做。你是在侮辱用户的浏览器,如果他们的浏览器可以处理你的脚本,你就不会有任何想法。并且

此外,有多少用户会通过脚本知道你的意思?


* * * * * * * *返回;

* * *}}


函数verify(){

* * if(doc1.readyState! = 4){

* * * *返回false;

* *}}



此功能出现是胡说八道。


[snip]


11月2日晚上11:30 * pm,David Mark< dmark。 cins ... @ gmail.comwrote:


11月2日下午3:10 *,Steve< stephen.jo ... @ googlemail.comwrote:


在这个新闻组和谷歌的帮助下我得到了这个代码

完全在Firefox中运行并可以在IE中提醒XML但是因为IE

没有暗示DOMgetElementsByTagNameNS()函数I

无法读取Cube命名空间中的各个速率。



目前,MS XML DOM对象不支持

getElementsByTagNameNS方法,但它肯定能够读取

来自文档中元素的数据。 *请参阅getElementsByTagName,

selectNodes和selectSingleNode。

http://msdn.microsoft.com/en-us/libr...28(VS.85).aspx
< blockquote class =post_quotes>
是否有一个包装器或其他一些相对简单的方法来获取

IE在Firefox中做什么是直截了当的?


这是代码。任何帮助感激不尽。


var doc

function load(){

* * if(document.implementation) &&

document.implementation.createDocument){



始终使用typeof来测试主机方法(例如,应该是对象) ,

" function"或unknown。)*通过布尔类型测试主机方法

转换已知会导致IE中的异常。



当然,如果它是对象然后还测试它是否是

" truthy" (否则为null传递。)*不*测试真实性。

" unknown"类型因为它们总是抛出异常。它还应该指出未知。不是一种方法的证据,只有证据证明某些东西存在。从DOM中删除元素

将许多属性(例如offsetParent)设置为unknown。类型。


在组中搜索isHostMethod为了安全的包装。传递它名称

的宿主对象属性已知作为方法实现

为IE'的未知类型使它成为一个不可靠的测试(例如

offsetParent不可调用。)


这个脚本只是我的第一个让IE读取的草稿一个XML文件,

支持W3C

DOM Level 2 Core的浏览器很简单。代码混乱的原因是因为我已经进行了大量的更改以使IE甚至加载XML。这就是为什么我要求新闻组寻求帮助。
谢谢回答,但我很害怕

以下回复并没有回答我的问题,这是有没有

a包装或其他一些相对简单的方法让IE做什么

Firefox中的内容是直截了当的?我将回复

答案的一些部分,这些部分很容易回答,与我问的

问题无关。


11月3日上午5:30,David Mark< dmark.cins ... @ gmail.comwrote:

11月2日下午3:10,Steve< ; stephen.jo ... @ googlemail.com写道:


是否有一个包装器或其他一些相对简单的方法来获取

IE在Firefox中做什么是直截了当的?



这是我希望得到答案的问题。


>


这是代码。任何帮助感激不尽。


var doc

function load(){

if(document.implementation& &

document.implementation.createDocument){



始终使用typeof来测试主机方法(例如,应该是object,

" function"或unknown。)通过布尔类型测试主机方法

转换已知会导致IE中出现异常。



这不会导致异常。


>


doc = document.implementation.createDocument("","",null);

doc.load(' 'CEBrates.xml'');

doc.onload = createTable;

}

else if(window.ActiveXObject){



我会测试的不仅仅是真实性这里(应该是一个函数。)

你将需要一个try-catch子句来处理不允许使用

ActiveX对象的情况,XML DOM对象是发生故障



实例化可能出错的其他任何事情。



OK


var doc1 = new ActiveXObject(" Microsoft) .XMLDOM");

函数loadXML(xmlFile){

doc1.async =" false" ;;



为什么要为字符串设置布尔属性?你很幸运

对象没有抛出异常。无法知道未来的Windows

更新是否会破坏这一点。



幸运?


doc1.onreadystatechange = verify;

doc1.load(xmlFile);

doc = doc1.documentElement;

}

loadXML(''CEBrates。 xml'');

alert(doc.xml)



使用window.alert。



为什么?甚至在Rhino书中也随处可见警报。


}

else {

alert(''你的浏览器不能处理这个脚本'');



永远不要这样做。你是在侮辱用户的浏览器,如果他们的浏览器可以处理你的脚本,你就不会有任何想法。并且

此外,有多少用户会通过脚本知道你的意思?



这只是在脚本工作之前。没有人会把它看作

它只在我的本地主机上。


function verify() {

if(doc1.readyState!= 4){

返回false;

}}



这个函数似乎是无意义的。



这个函数完全没问题。就绪状态为:

0对象未初始化

1加载对象正在加载数据

2已加载对象已加载数据

3来自对象的数据可以使用

4对象完全初始化


问候,史蒂夫。

With the help of this newsgroup and Google I have got this code
working fully in Firefox and can alert the XML in IE but because IE
does not impliment the DOM "getElementsByTagNameNS()" function I
cannot read the individual rates from the Cube namespace.

Is there a wrapper or some other relatively simple method of getting
IE to do what in Firefox is straighforward?

Here is the code. Any help gratefully received.

var doc
function load() {
if (document.implementation &&
document.implementation.createDocument){
doc = document.implementation.createDocument("", "", null);
doc.load(''CEBrates.xml'');
doc.onload = createTable;
}
else if (window.ActiveXObject){
var doc1 = new ActiveXObject("Microsoft.XMLDOM");
function loadXML(xmlFile){
doc1.async="false";
doc1.onreadystatechange=verify;
doc1.load(xmlFile);
doc=doc1.documentElement;
}
loadXML(''CEBrates.xml'');
alert(doc.xml)
}
else {
alert(''Your browser can\''t handle this script'');
return;
}
}
function verify() {
if (doc1.readyState != 4 ){
return false;
}
}
function createTable() {
var cubes = doc.getElementsByTagNameNS(''http://www.ecb.int/
vocabulary/2002-08-01/eurofxref'',''Cube'');
var dateRate = cubes[1].getAttribute(''time'');
var dateRateSplit = dateRate.split(''-'');
for (var i = 2; i < cubes.length; i++){
var currency = cubes[i].getAttribute(''currency'');
var rate = cubes[i].getAttribute(''rate'');
rateObject[currency] = rate;
}
document.getElementById(''boldStuff'').innerHTML = dateRateSplit[2]
+ "." + dateRateSplit[1] + "." + dateRateSplit[0];
document.getElementById("currency").value = "GBP";
getRates();
};
var rateObject = {};
function getRates() {
var curr= document.getElementById("currency").value;
var currRate = rateObject[curr];
var currStatement= "1 EUR = " + currRate + " " + curr ;
document.getElementById("rate").value=currStatemen t;
var currRev=1/currRate;
currRevFix=currRev.toFixed(5);
var currRevStatement= "1 " + curr +"= " + currRevFix + " EUR";
document.getElementById("rateRev").value=currRevSt atement;
};

解决方案

On Nov 2, 3:10*pm, Steve <stephen.jo...@googlemail.comwrote:

With the help of this newsgroup and Google I have got this code
working fully in Firefox and can alert the XML in IE but because IE
does not impliment the DOM "getElementsByTagNameNS()" function I
cannot read the individual rates from the Cube namespace.

At the present time, the MS XML DOM object does not support
getElementsByTagNameNS method, but it is certainly capable of reading
data from elements in the document. See getElementsByTagName,
selectNodes and selectSingleNode.

http://msdn.microsoft.com/en-us/libr...28(VS.85).aspx

>
Is there a wrapper or some other relatively simple method of getting
IE to do what in Firefox is straighforward?

Here is the code. Any help gratefully received.

var doc
function load() {
* *if (document.implementation &&
document.implementation.createDocument){

Always use typeof to test host methods (e.g. should be "object",
"function" or "unknown".) Testing host methods by boolean type
conversion is known to cause exceptions in IE.

* * * * * * * * doc = document.implementation.createDocument("", "", null);
* * * * * * * * doc.load(''CEBrates.xml'');
* * * * * * * * doc.onload = createTable;
* * * * }
* * * * else if (window.ActiveXObject){

I would test for more than "truthiness" here (should be a function.)
And you are going to need a try-catch clause to deal with cases where
ActiveX objects are disallowed, the XML DOM object is malfunctioning
or any of the other dozen things that can go wrong with the
instantiation.

* * * * * * * * var doc1 = new ActiveXObject("Microsoft..XMLDOM");
* * * function loadXML(xmlFile){
* * * * *doc1.async="false";

Why are you setting a boolean property to a string? You are lucky the
object doesn''t throw an exception. No way to know if a future Windows
update will break this.

* * * * *doc1.onreadystatechange=verify;
* * * * *doc1.load(xmlFile);
* * * * *doc=doc1.documentElement;
* * * }
* * * loadXML(''CEBrates.xml'');
* * * alert(doc.xml)

Use window.alert.

* * * }
* * * else {
* * * * * * * * alert(''Your browser can\''t handle this script'');

Never do this. You are insulting the user''s browser and you haven''t
the slightest idea if their browser can handle your script. And
besides, how many of your users would know what you mean by "script?"

* * * * * * * * return;
* * *}}

function verify() {
* * if (doc1.readyState != 4 ){
* * * * return false;
* *}}

This function appears to be nonsense.

[snip]


On Nov 2, 11:30*pm, David Mark <dmark.cins...@gmail.comwrote:

On Nov 2, 3:10*pm, Steve <stephen.jo...@googlemail.comwrote:

With the help of this newsgroup and Google I have got this code
working fully in Firefox and can alert the XML in IE but because IE
does not impliment the DOM "getElementsByTagNameNS()" function I
cannot read the individual rates from the Cube namespace.


At the present time, the MS XML DOM object does not support
getElementsByTagNameNS method, but it is certainly capable of reading
data from elements in the document. *See getElementsByTagName,
selectNodes and selectSingleNode.

http://msdn.microsoft.com/en-us/libr...28(VS.85).aspx

Is there a wrapper or some other relatively simple method of getting
IE to do what in Firefox is straighforward?

Here is the code. Any help gratefully received.

var doc
function load() {
* *if (document.implementation &&
document.implementation.createDocument){


Always use typeof to test host methods (e.g. should be "object",
"function" or "unknown".) *Testing host methods by boolean type
conversion is known to cause exceptions in IE.

And, of course, if it is "object" then also test if it is
"truthy" (else null passes.) Do *not* test the "truthiness" of
"unknown" types as they will always throw exceptions. It should also
be pointed out that "unknown" is not evidence of a method, only
evidence that something is there. Removing an element from the DOM
sets lots of properties (e.g. offsetParent) to "unknown" types.

Search the group for "isHostMethod" for a safe wrapper. Pass it names
of host object properties that are known to be implemented as methods
as IE''s "unknown" types make it an unreliable test otherwise (e.g.
offsetParent is not callable.)


This script is simply my first draft to get IE to read an XML file,
something that is straightforward with browsers that support the W3C
DOM Level 2 Core. The reason the code is messy is because I have had
to make lots of changes to get IE to even load the XML. This is why I
am asking the newsgroup for help. Thanks for answering but I''m afraid
the following response doesn''t answer my question, which was "Is there
a wrapper or some other relatively simple method of getting IE to do
what in Firefox is straighforward?" I will reply to some parts of the
answer which are easy to answer and have nothing to do with the
question I asked.

On Nov 3, 5:30 am, David Mark <dmark.cins...@gmail.comwrote:
On Nov 2, 3:10 pm, Steve <stephen.jo...@googlemail.comwrote:

Is there a wrapper or some other relatively simple method of getting
IE to do what in Firefox is straighforward?

This is the question I would appreciate an answer to.

>

Here is the code. Any help gratefully received.

var doc
function load() {
if (document.implementation &&
document.implementation.createDocument){


Always use typeof to test host methods (e.g. should be "object",
"function" or "unknown".) Testing host methods by boolean type
conversion is known to cause exceptions in IE.

This is not causing an exception.

>

doc = document.implementation.createDocument("", "", null);
doc.load(''CEBrates.xml'');
doc.onload = createTable;
}
else if (window.ActiveXObject){


I would test for more than "truthiness" here (should be a function.)
And you are going to need a try-catch clause to deal with cases where
ActiveX objects are disallowed, the XML DOM object is malfunctioning
or any of the other dozen things that can go wrong with the
instantiation.

OK

var doc1 = new ActiveXObject("Microsoft.XMLDOM");
function loadXML(xmlFile){
doc1.async="false";


Why are you setting a boolean property to a string? You are lucky the
object doesn''t throw an exception. No way to know if a future Windows
update will break this.

Lucky?

doc1.onreadystatechange=verify;
doc1.load(xmlFile);
doc=doc1.documentElement;
}
loadXML(''CEBrates.xml'');
alert(doc.xml)


Use window.alert.

Why? Alert is used everywhere even in the Rhino book.

}
else {
alert(''Your browser can\''t handle this script'');


Never do this. You are insulting the user''s browser and you haven''t
the slightest idea if their browser can handle your script. And
besides, how many of your users would know what you mean by "script?"

This is only there until the script is working. No-one will see it as
it is only on my localhost.

function verify() {
if (doc1.readyState != 4 ){
return false;
}}


This function appears to be nonsense.

This function is perfectly ok. Ready states are:
0 Object is not initialized
1 Loading object is loading data
2 Loaded object has loaded data
3 Data from object can be worked with
4 Object completely initialized

Regards, Steve.


这篇关于在IE中使用命名空间解析XML。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆